0
我已在laravel查詢以下訂購集合和關係集合laravel
$user = $this->user; //current user (dispatcher)
//obtain list of dispatchers,trucks,deliveries,drivers,trailers
$dispatchers = $this->instance->user()->where('active', 1)->with(['truck.delivery' => function($query) use ($week_array) {
$query->where('delivery_date', '>=', Carbon\Carbon::parse($week_array[1]['date'])->toDateTimeString())->whereNotNull('driver_id')
->orderBy('delivery_date');
}])->with(['truck.trailer', 'truck.driver', 'truck.driver2', 'truck.trailer.trailerType'])->get()
->sortBy('name')->sortBy(function ($item) use ($user) {
return $item->id == $user->id ? 1 . $item->name : 2 . $item->name;
});
我的目標是讓當前用戶是集合中的第一個項目,然後,我想有這個集合是按字母順序排列用戶名。
此外,這是我遇到問題的地方,我需要按字母順序排序每個用戶的驅動程序(子集的子集)。
排序整個集合級別1:$user fist, and then by $dispatchers->name
排序第2級爲集合中的每個項目:$dispatchers->truck->driver->first-name
我能夠創造一種爲1級(在當前用戶總是亮起頂部),但我無法爲每個用戶排序集合。
最終的結果應該是像(訂購用戶和他們的卡車通過收集驅動程序名稱驅動程序集合中訂購)
CURRENT USER (not matter the name)
Truck 50
Driver AA
Truck 45
Driver BB
Truck 56
Driver CC
USER A
Truck 10
Driver A
Truck 6
Driver B
Truck 20
Driver C
USER B
Truck 2
Driver D
Truck 3
Driver E
Truck 1
Driver F
USER C
Truck 30
Driver G
Truck 35
Driver H
Truck 13
Driver Z
等