Laravel Multiple Where Condition Example: In this tutorial you will lean how to use multiple where query with condition based with wherehas, whereIn and whereNotIn etc in laravel.
let’s see the where eloquent query condition with when and orwhere eloquent query example in laravel. You can use Multiple Where Condition Example laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 version.
Syntax:
Model::where('column_name', 'operator', 'value');
Example 1:
This example we have used two where query:
public function index()
{
$users = User::where('active', '=', 1)
->where('id', '>', 1)
->get();
dd($users);
}
Example 2:
You can use the multple where clouse using like this:
public function index()
{
$users = User::where([
['active', '=', 1],
['id', '>', 1]
])->get();
dd($users);
}
Example 3:
Many times we need to conditioned base where clouse, so we have added the where query with whereHas conditioned based here below example;
public function index()
{
$users = User::where('active', '=', 1)
->whereHas('roles', function($query){
$query->where('name', 'user');
})->get();
dd($users);
}
Example 4:
If you need multiple to multiple where query with conditioned with whereIn, when, whereNotIn then you can use the below example;
public function index()
{
$isAdmin = Auth::user()->hasRole('admin');
$users = User::where('active', '=', 1)
->whereHas('roles', function($query) {
$query->when($isAdmin, function($query) {
->whereNotIn('name', ['super-admin']);
})
->where('name', '=' ,'user');
})->get();
dd($users);
}
I hope these above example help you… if you have any suggest please comment us.