The whereIn
method is used to filter a query by checking if a given column’s value is contained within a given array. Here’s an example of how you might use it in a Laravel application:
1 2 3 4 5 | $users = DB::table('users') ->whereIn('id', [1, 2, 3]) ->get(); |
This will select all users with an id
of 1, 2, or 3.
You can also use the whereNotIn
method to select all rows that have a column value that is not contained within a given array.
1 2 3 4 5 | $users = DB::table('users') ->whereNotIn('id', [1, 2, 3]) ->get(); |
This will select all users with an id
that is not 1, 2, or 3.
In Eloquent (Laravel’s ORM), the whereIn
method can be used to filter a query by a given column’s values. Here’s an example:
1 2 3 | $users = User::whereIn('id', [1, 2, 3])->get(); |
This would return all users whose id
is 1
, 2
, or 3
.
You can also use whereNotIn
to filter out results where the given column’s value is in the given array.
1 2 3 | $users = User::whereNotIn('id', [1, 2, 3])->get(); |
This would return all users whose id
is NOT 1
, 2
, or 3
.
You can also chain other constraints onto the query, like so:
1 2 3 4 5 | $users = User::whereIn('id', [1, 2, 3]) ->where('age', '>', 18) ->get(); |
This would return all users whose id
is 1
, 2
, or 3
and whose age
is greater than 18
.