I'm trying to combine and sort the results from several db queries.
$events = collect();
$downedEvents = EventDowned::where('mission', $missionId)
->orderBy('mission_time', 'asc')
->get();
$events->push($downedEvents);
$getInOutEvents = EventGetInOut::where('mission', $missionId)
->orderBy('mission_time', 'asc')
->get();
$events->push($getInOutEvents);
$missileEvents = EventMissile::where('mission', $missionId)
->orderBy('mission_time', 'asc')
->get();
$events->push($missileEvents);
$flattenedEvents = $events->flatten();
$sortedEvents = $flattenedEvents->sortBy('mission_time');
return $sortedEvents->all();
The result looks like this:
As you can see it has correctly combined the results, however they remain in their original query order, not sorted.
I've also tried
$sortedEvents = $flattenedEvents->sortBy(function($event) {
return (int) $event->mission_time;
});
sortBy
and try. laravel.com/docs/5.4/collections#method-sortby – Candace$sortedEvents = $flattenedEvents->sortBy(function($event) { return $event->mission_time; });
but it has the same result – Interglacial$event->mission_time
and check the type – Candacereturn (int) $event->mission_time
with the same result – InterglacialsortBy
code. Don't worry it's a function of 10 lines. not a big deal – Candace