Laravel: order by where in

前端 未结 3 1833
盖世英雄少女心
盖世英雄少女心 2020-12-08 04:59

I am using SphinxSearch to query some contents and have the ids of my objects that I want to query with MySQL. The array of my ids are sorted depending on their rank Sphinx

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 05:29

    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();
    

提交回复
热议问题