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.
Here's an example:
<?php
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
This will generate a query similar to the following:
select * from users where id in (1, 2, 3);
Watch a video course
Learn object oriented PHP
If you are using Eloquent models, you can use the whereIn
method on a model's query builder instance:
$users = User::whereIn('id', [1, 2, 3])->get();
You can also use the whereIn
method on a relationship's query builder instance:
$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.