简体   繁体   中英

Query Laravel Select WhereIn Array

This query does not work correctly, it only shows 1 row

 $data = Post::select('id', 'name')
   ->whereIn('id', [$order])
   ->orderByRaw(\DB::raw("FIELD(id, $order)"))
   ->get();

but this works fine, it shows all rows

  $data = Post::select('id', 'name')
    ->whereIn('id', [1,2,3])
    ->orderByRaw(\DB::raw("FIELD(id, $order)"))
    ->get();

Thank you!

Your Query is Here:-

$data = Post::select('id', 'name')
  ->whereIn('id', $order)
  ->orderByRaw(\DB::raw("FIELD(id, ".implode(",",$order).")"))
  ->get();

Remove [] from $order .

For WhereIn condition second parameter should be an array. So the $order should be

$order = [1,2,3,4]

If your $order is an array, i think that you should do this

whereIn('id', $order) instead of whereIn('id', [$order])

PS In official documentation mentioned that second argument should be an array:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM