W3docs

How to Make Laravel Eloquent "IN" Query?

You can use the whereIn method on a query builder instance to create an "IN" clause for a given column and values.

You can use the whereIn method on a query builder instance to create an "IN" clause for a given column and values.

Here's an example:

Example of using the "whereIn" method on a query builder instance to create an "IN" clause for a given column and values in Laravel Eloquent

<?php

$users = DB::table('users')
  ->whereIn('id', [1, 2, 3])
  ->get();

This will generate a query similar to the following:

Example of a query to create an "IN" clause for a given column and values in Laravel Eloquent

select * from users where id in (1, 2, 3);

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

If you are using Eloquent models, you can use the whereIn method on a model's query builder instance:

Example of using the "whereIn" method on a model's query builder instance to create an "IN" clause for a given column and values in Laravel Eloquent

$users = User::whereIn('id', [1, 2, 3])->get();

You can also use the whereIn method on a relationship's query builder instance:

Example of using the "whereIn" method on a relationship's query builder instance to create an "IN" clause for a given column and values in Laravel Eloquent

$users = User::has('posts')->whereIn('id', [1, 2, 3])->get();

This will return all users who have posts and whose id is in the given array.