Laravel whereIn method query example

Sometimes you might want to get records by array instead of specific value. Laravel whereIn() method uses SQL WHERE IN statement.

In this tutorial article, we are going to see Laravel whereIn eloquent query example. This will compare field with array instead of value. Laravel whereIn() method works well with Eloquent as well as query builder. Let's see both example for whereIn() method. Laravel whereIn method works well in get(), update() or delete() method.

WhereIn() method in Laravel Eloquent

In this example, we will use User model to get specific users with array if ids.

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $users = User::whereIn('id', [1, 3, 5])
        ->get();
    
    dd($users);
}

WhereIn() method in query builder

In this example, we will use Illuminate\Support\Facades\DB facade to return specific users with array of ids.

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $users = \DB::table('users')
        ->whereIn('id', [1, 3, 5])
        ->get();
    
    dd($users);
}

I hope it will help you.

Tags: