Laravel Eloquent Query | Laravel Wherein Query Example

If you need to use SQL wherein query in laravel then you can use with the array. Laravel provides wherein() to use SQL wherein query. in wherein() we just need to pass two arguments one is the column name and another is an array of ids or anything that you want.

You can see below syntax on wherein query in laravel:

whereIn(Coulumn_name, Array);

Now I will give you three examples of how to use wherein query in laravel application. So let’s see those examples how works.

SQL Query

SELECT *
FROM users
WHERE id IN (4, 5, 6)

Laravel Query

public function index(){$users = User::select("*")
->whereIn('id', [4, 5, 6])
->get();
dd($users);
}

Example 2

Here is another example of wherein query. You can work with comma-separated string values. you can work as like bellow

public function index(){$myString = '1,2,3';
$myArray = explode(',', $myString);
$users = User::select("*")
->whereIn('id', $myArray)
->get();
dd($users);}

Example 3

public function index(){$users = DB::table('users')
->whereIn('name', ['Hardik', 'Vimal', 'Harshad'])
->get();
dd($users);}