mardi 21 février 2017

Ordering the result set laravel

I have the following laravel query

    $user = $this->user; //current user (dispatcher)

    //obtain list of dispatchers,trucks,deliveries,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 set, and then, I want to have this set ordered alphabetically by user name. In addition, this is where I am having a problem, I need to sort each user's drivers 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. enter image description here



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kK1gup
via IFTTT

Aucun commentaire:

Enregistrer un commentaire