mardi 6 décembre 2016

Laravel order data by relation property

I have two related models 'Yards' & 'Inspections'. A yard can have multiple inspections.

I want to be able to order the Yard list based on the inspection property 'reinspect_at'. I have a Model method that returns the Latest Inspection for the yard based on the inspection date.

I have tried the following:

$yards = Yard::orderBy('quarantined', 'DESC')
        ->with(['latestInspection' => function($query){
            $query->orderBy('reinspect_at', 'asc');
        }])
        ->get();

But this doesn't order the yard based on the reinspect_at date, it is only ordering the latest inspection for each yard.

I want to be able to always return the latest inspection for the yard, but need to be able to reorder the yard list based on the reinspect_at date for the latest inspection for each yard. Cheers



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire