3

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.

enter image description here

** 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.

2
  • i am using datatable plugin for sorting...can you show a screenshot of a result that you get Commented Dec 6, 2016 at 20:52
  • Have added a screenshot Commented Dec 6, 2016 at 21:41

3 Answers 3

1

Looks like I can do the following:

$yards = Yard::orderBy('quarantined', 'DESC')
        ->orderBy('archived', 'ASC')
        ->orderBy('name', 'ASC')
        ->get();
$yards = $yards->sortByDesc('latestInspection.reinspect_at');
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this line: $yards = $yards->sortByDesc('latestInspection.reinspect_at'); Sorts it based on the relation date
0

For example:

function latestInspection() {
   return $this->hasOne('App\Yard')->orderBy('reinspect_at', 'asc');
}

And

$yards = Yard::with('latestInspection')->orderBy('quarantined', 'DESC')->get();

1 Comment

Hi, this way only orders the inspection for each yard, I want to order the yard list based on the reinspect_at date from the latest inspection. i.e yard 1 - 01-01-2016 yard 2 - 02-01-2016
0

You could use some of the methods of the returned collection. Generally you want to handle this all on the database end but I think this way is worth it because it greatly simplifies the process.

$yards = Yard::with(['inspections'])->get()->sortBy(function($yard) {
    return $yard->inspections->max('reinspect_at');
});

I removed your ordering by quarantine. Not sure how you want to handle that if you are also sorting by the reinspect_date.

1 Comment

Hi thanks for the reply, this doesn't seem to make a difference to the ordering of the list of yards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.