Here is how you can have multiple where clause in your Eloquent query builder
$query->where([
['column_1', '=', 'value_1'],
['column_2', '<>', 'value_2'],
[COLUMN, OPERATOR, VALUE],
...
])
Laravel Eloquent Multiple Where Example
This will group the where conditions by AND
condition for example
User::where([
['billable','=','1'],
['is_active','=','1'],
['username','like','%tushar%']
])->get();
Will form a SQL query
SELECT * FROM users
WHERE
(`billable` = '1' AND `is_active` = '1' AND `username` like '%tushar%'
Laravel Eloquent where multiple OR clause
If you want to form a SQL query with multiple where you require multiple where with OR clause. Here is an example of that
User::where('city','=','Berlin')
->orWhere('city', '=', 'Chicago')
->orWhere('city','=','New York')->get();
Will form a SQL query
SELECT * FROM USERS
WHERE `city`= "Berlin" OR `city` = "Chicago" OR 'city' = "New York"
Laravel Eloquent where multiple AND and OR clause
If you are looking to form a query that contains multiple where clause with AND and OR conditions, Here is how you can do it.
For example if you have to translate following query in Eloquent
SELECT * FROM Users
WHERE Country='Germany' AND (City='Berlin' OR City='München');
This is how you can do it
User::where([['Country', '=', 'Germany']])
->where(function($q){
$q->where('city', '=', 'Berlin')
->orWhere('city', '=','München');
})->get();
That's all about Eloquent Multiple where clause examples.
Comments