raw('MAX(id) AS id'), app('flarum.db')->raw('SUM(is_read = 0) AS unread_count') ) ->where('user_id', $user->id) ->whereIn('type', $user->getAlertableNotificationTypes()) ->where('is_deleted', false) ->groupBy('type', 'subject_id') ->orderByRaw('MAX(time) DESC') ->skip($offset) ->take($limit); return Notification::with('subject') ->select('notifications.*', 'p.unread_count') ->mergeBindings($primaries->getQuery()) ->join(app('flarum.db')->raw('('.$primaries->toSql().') p'), 'notifications.id', '=', 'p.id') ->latest('time') ->get(); } }