1

Good day,

I wanted to do something like this:

$ids = [1, 2, 3];
$posts = $user->posts()->whereIn('id', $ids)->get();

but I get an Internal Server Error. Is there a different way to do this?

For the sake of this example, assume ID is not the primary key and that multiple posts can have the same ID. So this:

$ids = [1, 2, 3];
$posts = Post::whereIn('id', $ids)->get();

wouldn't return the desired results.

Lukho Mdingi
  • 518
  • 1
  • 5
  • 18

2 Answers2

3

The reason you are having the error is mysql is unable to what table you are referring with id column. Use tableName in whereIn() clouse

$posts = $user->posts()->whereIn('posts.id', $ids)->get();

N.B: I assume your Post table name is posts

Sohel0415
  • 9,523
  • 21
  • 30
0

please use like

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

link : Laravel ->where('id', ARRAY). multiple where conditions laravel 4

And

  User::select('id','name','email','phone','created_at')->whereIN('id',[1,2,3,4,5,6,7,8,9,9,10,11])->where('is_guest',NULL)->where('user_type','1')->orderBy('id','desc')->take(20)->get();

also work with laravel 5.4

Affan
  • 1,132
  • 7
  • 16