vendredi 24 mai 2019

groupBy is removing eager loaded relations?

This works (Version A):

$applications=  Apply::with('user','newUser', 'requests.files', 'requests.mails', 'requests.rank.orgas', 'requests.mode.trainer')->get();
dd($applications);

Here is the output with all relations:

enter image description here

Now I would like to order the Apply models by latest request.updated_at. To do so, I added this (Version B):

$application = Apply::with('user','newUser', 'requests.files', 'requests.mails', 'requests.rank.orgas', 'requests.mode.trainer')
                                ->join('applyRequest', 'applyRequest.apply_id', '=', 'apply.id')
                                ->groupBy('apply.id')
                                ->orderByRaw('max(applyRequest.updated_at) desc')
                                ->get();

This resolves into the correct sql

"select * from `apply` inner join `applyRequest` on `applyRequest`.`apply_id` = `apply`.`id` group by `apply`.`id` order by max(applyRequest.updated_at) desc"

and the Apply models are correctly sorted. However, most relationships are not loaded anymore:

enter image description here

Basicly, only relation user is eager loaded, and the other relations newUser and requests.* are not loaded in version B (but in version A).

Any ideas how to fix this?



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2W81ONy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire