我正在尝试对多个数据库查询的结果进行组合和排序。
$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();
结果如下所示:
如您所见,它已正确组合结果,但它们仍保持原始查询顺序,未排序。
我也试过
$sortedEvents = $flattenedEvents->sortBy(function($event) {
return (int) $event->mission_time;
});