Laravel Eloquent WhereIn Query Example

laravel

Loading

Hello Artisans, Today we will see wherein query in Laravel eloquent, How laravel model works with wherein array query. Here i will tell you some examples of wherein laravel query builder. also you can use laravel eloquent where is array.

With these examples, you can use wherein eloquent method in all laravel versions Laravel7, laravel8, laravel9, Laravel10 and Laravel 11.

If you want to use your mysql wherein query in laravel then you can use with this array. Laravel provide wherein() eloquent method to use SQL wherein query. In wherein() eloquent query, you just need to pass two arguments, where one is column name and another if array of ids or anything that you want.

You can see bellow syntax on wherein query in laravel:

whereIn(Coulumn_name, Array);

Now, we will see three examples how we will use wherein eloquent query in laravel application. So let’s see these examples, how it works:

Our 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);                    
}

Here is another example of wherein query. You can work with comma separated string value. 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);                    
}
public function index()
{
    $users = DB::table('users')
                    ->whereIn('name', ['Kishan', 'Soniya', 'Salmaan'])
                    ->get();
  
    dd($users);                    
}

I hope this example of whereIn eloquent query builder help you to understand, how you can filter datas from a table using whereIn laravel query builder.

How to use jQuery Animation Effects

About Post Author