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
Post a Comment