vendredi 23 février 2018

How to get the last (per date) joined row for each result with Laravel Eloquent

Here is what I want to achieve :

I have a Formation model which have FormationSession model. I need all formations with, for each, the last session in database.

Here is what I got so far :

$formations = Formation::query()
    ->with('location')
    ->with('jobs')
    ->with('employee')
    ->with('intervenant.company')
    ->with(['formation_sessions' => function($query) {
        $query->limit(1)->orderBy('start_at', 'desc');
    }])
    ->whereNotNull('update_frequency')
    ->get();

I'm pretty stuck here, how can I achieve this ?



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

Aucun commentaire:

Enregistrer un commentaire