I have two queries, the first one gives me an array of ids, that is in a specific order. Then that array of ids I pass it to the second query like so:
Operation::whereIn('id', $ids)->get();
But when I output the result of that query, the order has changed, if the array $ids was something like (4,2,6,9) which is the order I wanted the results to be in, the output will give me 2,4,6,9. How can I avoid that?
MySQL way of sorting with order same as in where in
clause:
$ids; // array of ids
$placeholders = implode(',',array_fill(0, count($ids), '?')); // string for the query
Operation::whereIn('id', $ids)
->orderByRaw("field(id,{$placeholders})", $ids)->get();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments