Might I suggest using the whereBetween query then using Eloquent Collections to groupBy day, then pluck the last record for each (grouped) day?
How to get the last record of a day of date range
I have traffic table with many records during a day, and I need to get statistic for some date range, but only the last record of a day.
And I'm stuck a bit, because if I do like this
$startDate = date('Y-m-d', strtotime('2021-03-01')); $endDate = date('Y-m-d', strtotime('2021-03-24')); $records = Traffic::whereDate('created_at', '>=', $startDate)->whereDate('created_at', '<=', $endDate)->get();
That's how I see all records, but I need only for the of each day
$records = Traffic::all()->sortByDesc('created_at')->take(1)->toArray();
This give me just last record of table...
@impala44 You can get ids of last rows for each day and then , by these ids, get all rows which you need:
select created_at, max(id)
from traffics
where created_at between '2020-01-01' and '2020-01-31'
group by created_at
Please or to participate in this conversation.