GroupBy not Sorting by latest
Just to make this easy to find. I had a report to write which had numerous daily rows of the same related model data. So in this cause I was tracking servers and security reports.
My reports where daily like this
But a groupBy here on server_id would return ID 1 so it would appear as if it was still failing.
$results = $results->whereRaw('ID IN ( select MAX(id) from reports GROUP BY server_id)');
or double group by:
$results = $results->whereRaw('ID IN ( select MAX(id) from reports GROUP BY server_id, type)');
Did the trick to sort and group by that latest record, which
latest did not do for me.
comments powered by Disqus