Last active
February 15, 2019 10:51
-
-
Save gsabater/92df47beedc5d4865c6c5a732b71c878 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// 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 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// 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