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

Daniel-Pablo's avatar

laravel 8 Query for group and select a sub query and grouped from DB

Hi, in trying to make a query that make a group and works fine but now i'm trying to select from that group the only one that have a count greater than so the groups that have 0 counts doesn't appear ,can anyone help me please?

My group query:

'users_int' => Interaction::select('user_id')
      ->groupByRaw('user_id')
      ->get()

And my try to select the users and inside the user other group whit his actions

'users_int' => Interaction::select('user_id')
     groupBy('user_id', 'action_type')
      ->selectRaw('count(*) as total, user_id, action_type')  
      ->get()

this is the result

[{"total":17,"user_id":1,"action_type":"whatsapp"},
{"total":1,"user_id":2,"action_type":"phone"},
{"total":13,"user_id":2,"action_type":"whatsapp"}]

but i what it to group the actions by user, Help me please

0 likes
1 reply
jlrdw's avatar

Try a where after select, before group.

Please or to participate in this conversation.