You could try something like:
Sensor::with(['measurements' => function ($q)
{
$q->orderBy('measured_on', 'desc')->groupBy('sensor_id');
}])->get();
Let me know if that gets you close.
Good idea, but it does not work, since orderBy
is performed on the already grouped rows with a measured_on
value corresponding to the measurement that was first inserted into the db.
The ordering should have been performed before the grouping, but that is impossible I guess...
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community