motinska94's avatar

How to get records weekly -monthly -yearly by timestamps?

I need to get some information from the database to show on seperate weekly, monthly and yearly graphs. Is there a helper or a built-in eloquent tool to get them filtered by those formats? Like pagination, click a button to go previous week etc.

0 likes
3 replies
AungHtetPaing__'s avatar
Level 22

@motinska94 you can get weekly, monthly, yearly with collection groupby. I don't know about filtering by those formats.

$weeklyOrders = Order::query()
            ->whereYear('created_at', date('Y'))
            ->get()
            ->groupBy(fn($item) => $item->created_at->format('W')); // for monthly 'format('m')' for yearly 'format('Y')'
1 like

Please or to participate in this conversation.