Skip to content

Instantly share code, notes, and snippets.

@gsabater
Last active February 15, 2019 10:51
Show Gist options
  • Save gsabater/92df47beedc5d4865c6c5a732b71c878 to your computer and use it in GitHub Desktop.
Save gsabater/92df47beedc5d4865c6c5a732b71c878 to your computer and use it in GitHub Desktop.
<?
// Query Posts 3, 5, 22 and 7
$ids = [3, 5, 22, 7];
$ordered = implode(',', $ids);
$items = \App\Post::whereIn('id', $ids)
->orderByRaw(DB::raw("FIELD(id, $ordered)"))
->get();
<?
// Get all orders since January
// and get first the ones that need to be edited
$orders = DB::table('orders')
->where('created_at', '>', '2019-01-01')
->orderByRaw('updated_at - created_at DESC')
->get();
// Query would be
/*
select * from orders
where created_at > '2019-01-01'
order by (updated_at - created_at) DESC
*/
<?
// Query Posts 3, 5, 22 and 7 in the same order
$ids = [3, 5, 22, 7];
$items = \App\Post::whereIn('id', $ids)
->get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment