How can we Create Multiple Where Clause Query Using Laravel Eloquent?

0 votes

Hii,

I'm using the Laravel Eloquent query builder and I have a query where I want a WHERE clause on multiple conditions. It works, but it's not ingenious.

Example:

$results = User::where('this', '=', 1)
    ->where('that', '=', 1)
    ->where('this_too', '=', 1)
    ->where('that_too', '=', 1)
    ->where('this_as_well', '=', 1)
    ->where('that_as_well', '=', 1)
    ->where('this_one_too', '=', 1)
    ->where('that_one_too', '=', 1)
    ->where('this_one_as_well', '=', 1)
    ->where('that_one_as_well', '=', 1)
    ->get();

Is there a better way to do this, or should I stick with this method?

Thank you!!

Mar 30 in Laravel by kartik
• 26,540 points
2,181 views

1 answer to this question.

0 votes

Hii,

You can use Conditions using Array:

$users = User::where([
       'column1' => value1,
       'column2' => value2,
       'column3' => value3
])->get();

That Will produce query like bellow:

SELECT * FROM TABLE WHERE column1=value1 and column2=value2 and column3=value3;

Conditions using Antonymous Function:

$users = User::where('column1', '=', value1)
               ->where(function($query) use ($variable1,$variable2){
                    $query->where('column2','=',$variable1)
                   ->orWhere('column3','=',$variable2);
               })
              ->where(function($query2) use ($variable1,$variable2){
                    $query2->where('column4','=',$variable1)
                   ->where('column5','=',$variable2);
              })->get();

That Will produce query like bellow:

SELECT * FROM TABLE WHERE column1=value1 and (column2=value2 or column3=value3) and (column4=value4 and column5=value5);

Thank You!

answered Mar 30 by Niroj
• 57,260 points

Related Questions In Laravel

0 votes
1 answer

How can we get started with Laravel through Xampp?

Hii, First you need to start Apache and ...READ MORE

answered Mar 17 in Laravel by Niroj
• 57,260 points
70 views
0 votes
1 answer

What is middleware? How can we register middeleware in Laravel?

Middleware acts as a bridge between a ...READ MORE

answered Mar 18 in Laravel by Niroj
• 57,260 points
268 views
0 votes
1 answer

How can we Disable CSRF Laravel?

Hello, You can disable CSRF Laravel from the App/Http/Kernel.php file ...READ MORE

answered Mar 19 in Laravel by Niroj
• 57,260 points
77 views
0 votes
1 answer

How can we generate migration in Laravel?

Hello, Migrations are like version control for your database, ...READ MORE

answered Mar 19 in Laravel by Niroj
• 57,260 points
54 views
0 votes
1 answer

How to send email using php?

Hello @kartik 1.) Download PHPMailer, open the zip file ...READ MORE

answered Apr 1 in PHP by Niroj
• 57,260 points
80 views
0 votes
1 answer

Where to register Facades & Service Providers in Lumen?

Hello, To register a facade with an alias, ...READ MORE

answered Apr 6 in Laravel by Niroj
• 57,260 points
387 views
0 votes
1 answer

How can I update NodeJS and NPM to the next versions?

Hello @kartik, First check your NPM version npm -v 1).Update ...READ MORE

answered May 5 in Java-Script by Niroj
• 57,260 points
74 views
0 votes
1 answer

Switch statement for string matching in JavaScript

Hello @kartik, Just use the location.host property switch (location.host) ...READ MORE

answered Jun 2 in Java-Script by Niroj
• 57,260 points
60 views
0 votes
1 answer

How can we create a record in Laravel using eloquent?

We need to create a new model ...READ MORE

answered Mar 20 in Laravel by Niroj
• 57,260 points
91 views
0 votes
1 answer

How can we Create custom name while uploading image using storage?

Hello, You also can do like this $ImgValue ...READ MORE

answered Mar 26 in Laravel by Niroj
• 57,260 points
47 views