selectRaw('SUM(read_at IS NULL) AS unread_count') ->where('user_id', $user->id) ->whereIn('type', $user->getAlertableNotificationTypes()) ->where('is_deleted', false) ->whereSubjectVisibleTo($user) ->groupBy('type', 'subject_id') ->orderByRaw('MAX(created_at) DESC') ->skip($offset) ->take($limit); return Notification::select('notifications.*', 'p.unread_count') ->joinSub($primaries, 'p', 'notifications.id', '=', 'p.id') ->latest() ->get(); } /** * Mark all of a user's notifications as read. * * @param User $user * * @return void */ public function markAllAsRead(User $user) { Notification::where('user_id', $user->id)->update(['read_at' => Carbon::now()]); } }