user(); $page = max(1, (int) $request->query('page', 1)); $notifications = $user->notifications() ->latest() ->limit(200) // aggregate from last 200 raw notifs ->get(); $digested = $this->digest->aggregate($notifications); // Simple manual pagination on the digested array $perPage = 20; $total = count($digested); $sliced = array_slice($digested, ($page - 1) * $perPage, $perPage); $unread = $user->unreadNotifications()->count(); return response()->json([ 'data' => array_values($sliced), 'unread_count' => $unread, 'meta' => [ 'total' => $total, 'current_page' => $page, 'last_page' => (int) ceil($total / $perPage) ?: 1, 'per_page' => $perPage, ], ]); } public function readAll(Request $request): JsonResponse { $request->user()->unreadNotifications->markAsRead(); return response()->json(['message' => 'All notifications marked as read.']); } public function markRead(Request $request, string $id): JsonResponse { $notif = $request->user()->notifications()->findOrFail($id); $notif->markAsRead(); return response()->json(['message' => 'Notification marked as read.']); } }