$ids = [1, 5, 17, 23];
$products = Product::whereIn('id', $ids)->get();
->orderBy(\DB::raw("FIELD(id, " . implode(',', $entityIds) . ")"))
$ids = [1, 5, 17, 23];
$products = Product::whereIn('id', $ids)
->orderBy(\DB::raw("ORDER BY FIELD(id".implode(',',$ids).")"))
->get();
$products = Product::whereIn('id', $ids)
->orderBy('id', 'asc')
->get();
class X
{
public static function getRowsByField($rows, $field)
{
$output = [];
foreach ($rows as $row) {
$output[$row[$field]] = $row;
}
return $output;
}
}
$ids = [1, 5, 17, 23];
$products = Product::whereIn('id', $ids)->get();
$productsById = X::getRowsByField($products, 'id');
foreach ($ids as $id) {
....$productsById[$id]...
}