user() ->notifications() ->latest() ->paginate(20); return view('notifications.index', compact('notifications')); } public function unread(Request $request): JsonResponse { $notifications = $request->user() ->unreadNotifications() ->latest() ->take(10) ->get() ->map(fn ($n) => [ 'id' => $n->id, 'type' => class_basename($n->type), 'title' => $n->data['title'] ?? 'Notification', 'message' => $n->data['message'] ?? '', 'icon' => $n->data['icon'] ?? 'bell', 'url' => $n->data['url'] ?? null, 'created_at' => $n->created_at->diffForHumans(), ]); return response()->json([ 'notifications' => $notifications, 'unread_count' => $request->user()->unreadNotifications()->count(), ]); } public function markAsRead(Request $request, string $id): JsonResponse { $notification = $request->user() ->notifications() ->where('id', $id) ->first(); if ($notification) { $notification->markAsRead(); } return response()->json(['success' => true]); } public function markAllAsRead(Request $request): JsonResponse { $request->user()->unreadNotifications->markAsRead(); return response()->json(['success' => true]); } }