Created
August 30, 2018 05:49
-
-
Save omitobi/5fb47b68e8fd8635022d77fc1689d2e6 to your computer and use it in GitHub Desktop.
maintain the order of queried records
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
$users_ids = [1,3,2]; | |
$items = collect($users_ids); | |
$fields = $items->map(function ($ids){ | |
return '?'; | |
})->implode(','); | |
$ordered = User::orderbyRaw("FIELD (id, ".$fields.")", $items->prepend('id')) | |
->find($users_ids)->pluck('id'); | |
//ordered should be [1,3,2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment