user()->notifications()->limit(20)->get(); return response()->json([ 'notifications' => $notifications->map(fn (DatabaseNotification $n) => [ 'id' => $n->id, 'title' => (string) ($n->data['title'] ?? ''), 'body' => (string) ($n->data['body'] ?? ''), 'icon' => (string) ($n->data['icon'] ?? 'bell'), 'has_url' => filled($n->data['url'] ?? null), 'read' => $n->read_at !== null, 'created_at' => $n->created_at?->toIso8601String(), ]), 'unread' => $request->user()->unreadNotifications()->count(), ]); } public function open(Request $request, string $id): RedirectResponse { $notification = $request->user()->notifications()->whereKey($id)->firstOrFail(); $notification->markAsRead(); $url = (string) ($notification->data['url'] ?? ''); // Nur Adressen innerhalb des Portals ansteuern. return redirect()->to($url !== '' && (Str::startsWith($url, '/') && ! Str::startsWith($url, '//') || Str::startsWith($url, url('/'))) ? $url : '/'); } public function markAllRead(Request $request): JsonResponse { $request->user()->unreadNotifications()->update(['read_at' => now()]); return response()->json(['unread' => 0]); } }