We manage data in a relational database management system through different SQL queries. The whereIn is also a SQL query. In this tutorial, you are going to learn how to use SQL whereIn query in Laravel. Let’s learn through examples.

Suppose we have a customers table in our database. There are five columns in the table which are id, name, email, phone, and address. We also have five customer data stored in our table.

whereIn() method

Now, why are we going to use the whereIn query? Right? Let’s think, we need to pull out only two data from our database. We can use the whereIn query to do it.

The whereIn query takes two arguments. The first one is the column name from our table and the second one takes an array.

If you are working with Eloquent ORM then you can use as below-

public function view(){

      $customers = Customer::select("*")
                      ->whereIn('id', [1,5])
                      ->get();
      dd($customers);                    
}

In this example, we pass our column name which is id as first argument and id 1,5 as second argument because we want to extract all the information that belongs to id 1 ad 5.

Output:

How to use whereIn() method in Laravel

Here, you can see that there are two sets of data arrived as an array with all the information that belongs to id 1 and 5.

whereIn() method

If you want to use the Query builder:

public function view(){

      $customers = DB::table('customers')
                      ->whereIn('phone', ['22222', '44444'])
                      ->get();
      dd($customers);                    
}

In this example, we retrieved customer data according to their phone number.

Output:

So, that’s how you can use whereIn SQL query in Laravel. I hope, this will help you. Thank you.