currentWorkspace(); $notifications = $request->user() ->notifications() ->whereJsonContains('data->workspace_id', $workspace->id) ->latest() ->paginate(25); $declarationIds = collect($notifications->items())->pluck('data.declaration_id')->filter()->unique()->values(); $senderIds = collect($notifications->items())->pluck('data.sender_id')->filter()->unique()->values(); $declarations = Declaration::whereIn('id', $declarationIds)->pluck('title', 'id'); $senders = User::whereIn('id', $senderIds)->pluck('name', 'id'); $notifications->through(function ($n) use ($declarations, $senders) { $data = $n->data; $declarationId = $data['declaration_id'] ?? null; $senderId = $data['sender_id'] ?? null; $declarationExists = $declarationId && isset($declarations[$declarationId]); $data['declaration_title'] = $declarationExists ? $declarations[$declarationId] : null; $data['sender_name'] = $senderId ? ($senders[$senderId] ?? null) : null; $data['url'] = $declarationExists ? route('declarations.show', $declarationId) : null; return [ 'id' => $n->id, 'type' => class_basename($n->type), 'data' => $data, 'read_at' => $n->read_at?->toISOString(), 'created_at' => $n->created_at->diffForHumans(), ]; }); return Inertia::render('notifications/Index', [ 'notifications' => $notifications, 'markAllReadUrl' => route('notifications.readAll'), 'readUrl' => route('notifications.read', ['id' => '__ID__']), ]); } public function markAsRead(Request $request, string $id): RedirectResponse { $request->user() ->notifications() ->where('id', $id) ->firstOrFail() ->markAsRead(); $workspaceId = $request->session()->get('current_workspace_id'); Cache::forget("user:{$request->user()->id}:workspace:{$workspaceId}:unread_notifications"); return back(); } public function markAllAsRead(Request $request): RedirectResponse { $workspace = $this->currentWorkspace(); $request->user() ->unreadNotifications() ->whereJsonContains('data->workspace_id', $workspace->id) ->update(['read_at' => now()]); Cache::forget("user:{$request->user()->id}:workspace:{$workspace->id}:unread_notifications"); return back(); } }