duanchao4445 2017-02-22 02:34
浏览 45
已采纳

订购集合和关系集合laravel

I have the following laravel query

    $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;
    });

My goal is to have CURRENT user to be the first item in the collection, and then, I want to have this collection ordered alphabetically by the user name.

In addition, this is where I am having a problem, I need to sort each user's drivers (a subset of a subset) alphabetically.

Sort level 1 for the whole collection: $user fist, and then by $dispatchers->name

Sort level 2 for each item in the collection: $dispatchers->truck->driver->first-name

I was able to create a sort for Level 1 (when current user is always comes on top), but I am unable to sort a collection for each user.

The final result should be like (users ordered and their trucks collections ordered by driver names in driver collection)

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 

etc.

enter image description here

  • 写回答

1条回答 默认 最新

  • dongqian1028 2017-02-22 04:03
    关注
    $user = $this->user; //current user (dispatcher)
    
    //obtain list of dispatchers,trucks,deliveries,drivers,trailers
    $dispatchers = $this->instance->user()->where('active', 1)
        ->with([
            'truck' => function ($query) {
                $query->select(['trucks.*']);
                $query->join('drivers', 'trucks.id', '=', 'drivers.truck_id');
                $query->oldest('drivers.name');
            },
            '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;
        });
    

    It works for me, but not sure in your case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line