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
Screenshot of yard list display below. As you can see the Inspection due date is how I want to be able to order the result.
** Update ** Looks like what I want is possible in the frontend template using:
@foreach( $yards->sortBy('latestInspection.reinspect_at') as $yard )
It would be preferable to do be able to do this from the controller though, so I can have other options for ordering by.
