I have the following 2 tables:
Cars
----
id | created_at
and
Repaired
id | cars_id | updated_at
I want to display all cars, ordered by created_at desc, BUT also ordered by updated_at from the Repaired pivot table. Repaired can have many entries of specific cars_id.
So if carA is created_at 2017-02-20, and carB is created_at 2016-01-10, but the updated_at in Repaired for carB is on 2017-02-22, i want to display carB BEFORE carA, because the relation is "newer".
I also don't want do load the data from the relation table.
I tried the follwing:
$cars = $model->load(['repaired' => function($query) {
return $query->orderBy('updated_at', 'desc');
}])->orderBy('created_at', 'desc');
and
$model->join('repaired', 'repaired.cars_id', '=', 'cars.id')
->orderBy('repaired.updated_at', 'desc')
->orderBy('cars.created_at', 'desc')
->select('cars.*');
^ this query return all items from the Repaired table, even if the updated_at field is NULL. And i can't group them.