Laravel Eloquent Query | Laravel Multiple Where Condition Example
--
Today, I teach you how to write multiple “where” clauses in the laravel query builder. I will give you an example of laravel eloquent multiple where conditions. you can easily execute multiple where conditions in query with laravel 6, laravel 7, and laravel 8.
Almost, we need to write multiple where condition with laravel. as we know we can use the “where” clause using where() in laravel. but if you want to write a multiple where clause in laravel then I will give you two examples of how to write multiple where clauses in laravel.
You can see both syntaxes of writing multiple where condition:
->where('COLUMN_NAME', 'OPERATOR', 'VALUE')->where('COLUMN_NAME', 'OPERATOR', 'VALUE')
OR
->where([['COLUMN_NAME', 'OPERATOR', 'VALUE'],['COLUMN_NAME', 'OPERATOR', 'VALUE']]);
Now I will give you an example of how to write multiple “where” conditions with laravel.
If you have SQL query like as below with multiple where conditions:
SQL Query:
SELECT * FROM `users`
WHERE active = 1 AND is_ban = 0
Then you can write your SQL query like as below both ways:
Example 1:
public function index(){$users = User::select('*')
->where('active', '=', 1)
->where('is_ban', '=', 0)
->get();dd($users);
}
Example 2:
public function index(){$users = User::select('*')
->where([
['active', '=', 1],
['is_ban', '=', 0]
])
->get();dd($users);
}