[英]How do I count the number of unique rows in group by laravel?
我有三種通知類型TYPE_A
TYPE_B
TYPE_C
。 對於每種類型,單個用戶可能會有多個通知。 我想獲取用戶針對每種通知類型獲得的通知數量。 現在我有這個:
$notifications = Notification::where('user_id', $user->id)->groupBy('notification_type')->get();
如何計算每個notification_type
的行數?
您可以將raw()
方法用作:
$notifications = Notification::where('user_id', $user->id)
->select('*', DB::raw("count(*) as count"))
->groupBy('notification_type')
->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.