Laravel: the order in which

I use SphinxSearch to query for some content and have the identifiers of my objects that I want to query with MySQL. The array of my identifiers is sorted based on their Sphinx rank. Thus, I would like to make MySQL as follows:

SELECT * FROM table WHERE id IN (1,17,2) ORDER BY FIELD(id,1,17,2) 

I know that I can:

 Table::whereIn('id', $ids)->get(); 

But I can’t get the order I had.

How can I do this with Laravel?

+7
php mysql laravel sphinx
source share
1 answer

Using the solution found at 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(',', $itemIds); $items = static::whereIn('id', $ids) ->orderByRaw(DB::raw("FIELD(id, $ids_ordered)")) ->get(); 
+26
source share

All Articles