Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

haampie haampie Joined 5 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.