mysql - How can I select with count() on specific user in Laravel? -
here database structure:
also - there table users , reciever_id references table id. use query count of each type of notifications data type of notifications notification_types table.
notification:: select('notification_types.*', db::raw('count(*) total')) ->join('notification_types', 'notifications.type_id', '=', 'notification_types.id') ->groupby('notifications.type_id') ->get()
what need - set constraint on reciever_id, don't - should put clause?
just chain where
method anywhere before get
, since condition applied on notifications
table:
notification::select('notification_types.*', db::raw('count(*) total')) ->join('notification_types', 'notifications.type_id', '=', 'notification_types.id') ->where('reciever_id', $receiverid) ->groupby('type_id') ->get();
also, there's no need query group notifications.type_id
, type_id
do, because there no ambiguities created here because there no other columns named type_id
.
Comments
Post a Comment