E
E
ettychel2019-07-05 09:03:14
Laravel
ettychel, 2019-07-05 09:03:14

Sorting relation in laravel via load() doesn't work, what's wrong?

Good day!
Faced such a problem. I have a link that is dynamically pulled up, it is already in the main query, but when it is dynamically pulled up, I need to sort by this link. I read it in the documentation through load(), but it wasn’t there ... Call to undefined method Illuminate\Database\Eloquent\Builder::load()
Why does it swear so much in the model, I connected the class.
Now the request itself to the studio, I’ll write it down without logic when it connects there, because I don’t think that there can be some kind of jamb, because everything works as soon as I comment:

$this->startCondition()
                    ->select($columns)
                    ->orderBy('id','desc')
                    ->with(['statusName:status_id,status_name',
                            'workerName:id,fio', 
                            'armReq:id,title,user_id,priority,range_priority',
                            'getNameReqPeople'])
// Это нельзя вынести за запрос Начало
                    ->whereHas('armReq', function($q) {
                        $q->where('column', 'like', '%Значение%');
                    })
                    ->load(['armReq' => function ($q){
                        $q->orderBy('range_priority', 'desc');
                    }]);
// Конец
                    ->paginate(12);

So... I re-read the documentation, like if you do all() on the model, then you get a collection, so I mess with load(), although this method is in the class...
how can I get around this? There is one condition: you cannot move the block marked with a comment outside the query

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ettychel, 2019-07-05
@ettychel

I solved the problem, of course I think that this is not the best solution and therefore I am waiting for any suggestions, but this method works:

$columns = ['arm_jobs.id',
                    'worker',
                    'task',
                    'status',
                    'date_offer',
                    'start_time',
                    'deadline',
                    'timework'
        ];

$this->startCondition()
                    ->select($columns)
                    ->with(['statusName:status_id,status_name',
                            'workerName:id,fio', 
                            'armReq:id,title,user_id,priority,range_priority',
                            'getNameReqPeople'])
                    ->whereHas('armReq', function($q) {
                        $q->where('column', 'like', '%Значение%');
                    })
                   ->join('arm_req_posts as req', 'req.id', '=', 'arm_jobs.id')
                                            ->orderBy('req.range_priority','desc')
                    ->orderBy('arm_jobs.id','desc')
                    ->paginate(12);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question