php - Laravel Sort Collection By Dynamic ID array -


i have following...

$people = array(5, 2, 9, 6, 11);  $people_collection = people::find($people); 

but when dump , die $people_collection collection ordered id asc, how can keep collection, in same order $people array?

collections has sortby function takes custom callback:

$people_collection = people::find($people)    ->sortby(function($person, $key) use($people) {          return array_search($person->id, $people);       }); 

see the docs.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -