Jeyziii's avatar

Laravel orderBy

I need to order the query by edtr_item_arrangement from edtrDetails. Is this possible?

$this->getSweeperbookings = OimsTeamRouteAssignmentDetails::
                withCount(['EDTRReference'
                    => function ($query0) {
                    $query0->with(['edtrDetails' => function ($query02) {
                        $query02->with('edtrbook', 'edtrtransaction');
                    }]);
                    $query0->whereDate('dispatch_date', Carbon::today());
                }])
                ->with(['EDTRReference'
                    => function ($query1) {
                    $query1->with(['edtrDetails' => function ($query12) {
                        $query12->with('edtrbook', 'edtrtransaction');
                        $query12->orderBy('edtr_item_arrangement');
                    }]);
                    $query1->withCount(['edtrDetails as pickup' => function ($query11) {
                        $query11->where('edtr_item_type', 1);
                    }]);
                    $query1->withCount(['edtrDetails as delivery' => function ($query11) {
                        $query11->where('edtr_item_type', 2);
                    }]);
                    $query1->whereDate('dispatch_date', Carbon::today());
                }])
                ->where('id', $data['id'])->get();

relationship:

    public function EDTRReference()
    {
        return $this->hasMany(OimsEdtr::class, 'tra_id', 'id');
    }

    public function edtrDetails()
    {
        return $this->hasMany(OimsEdtrDetails::class, 'edtr_id', 'id');
    }
0 likes
5 replies
jlrdw's avatar

I suggest get the generated sql and see if it is correct.

1 like
Jeyziii's avatar

@Ben Taylor can you tell how. I also tried it, still can't get the right output.

enoch91's avatar

I believe it is, however, you need to use a combination of eager loading and modifying the query to sort the related data properly

1 like

Please or to participate in this conversation.