I have looked at most other solutions here but none of them seems to work for me. I have a table with about 100K rows in it.
each row is a record from an iot device, which includes the unique id of that device. The device id may have 10K rows from the 100K.
Reading::whereIn('device_id', $ids)->orderBy('created_at',
'DESC')->groupBy('device_id')->get()->
I am able to pull out the ids i need and group them together by "device_id" as query above but its picking the first result and not the last for each id.
How can i get the most recent record for each id instead of the oldest?
I have looked at laravel collections and the reverse(), first(), latest(); option but i still cant get it
Any ideas?
UPDATE
The best i have been able to come up with is this query below which does grab the latest record for each device_id
SELECT t1.* FROM readings t1 WHERE t1.id = (SELECT t2.id FROM readings
t2 WHERE t2.device_id = t1.device_id ORDER BY t2.id DESC LIMIT 1)
I'll then loop through those results and grab only the records the user has access to.
Not pretty, but it works for now.
If anyone ever does find away to do it in eloquent please answer