Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CH17's avatar
Level 3

Count with multiple group by

Hello, I have a table called activities that stores id, user_id, actions, created_at. I want to get action-count per user per day. Something like.

| Date | User ID | Action Count | | ------ | ----------- | ----------- | | 2020-03-12 | 2 | 12 | | 2020-03-12 | 3 | 15 | | 2020-03-13 | 2 | 19 | | 2020-03-13 | 3 | 10 |

Can you please suggest me how can I do that?

Thanks in advance.

0 likes
3 replies
CorvS's avatar

@ch17 In what way does your activities table store multiple actions?

MichalOravec's avatar
Level 75
$activities = DB::table('activities')
    ->selectRaw('user_id, date(created_at) as date, sum(actions) as actions'))
    ->groupByRaw('user_id, date(created_at)')
    ->get();

Everything what you need you find in the documentation

https://laravel.com/docs/8.x/queries

CH17's avatar
Level 3

Many thanks. With a little tweak It works!

Please or to participate in this conversation.