Laravel: order by where in

♀尐吖头ヾ 提交于 2019-11-28 05:58:02

Using the solution found on http://laravelsnippets.com/snippets/get-all-items-at-once-ordered-by-the-current-order-of-ids-in-the-where-in-clause-using-eloquent

$ids = array(1,17,2);

$ids_ordered = implode(',', $ids);

$items = static::whereIn('id', $ids)
 ->orderByRaw(DB::raw("FIELD(id, $ids_ordered)"))
 ->get();

I got this problem too, but my target array elements were strings. in this case ...

$strings = array('xxx','yyy','zzz');

$imploded_strings = implode("','", $strings);

$items = static::whereIn('some_column', $strings)
->orderByRaw(DB::raw("FIELD(some_column, '$imploded_strings')"))
->get();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!