Deploy HickLab3D Dashboard / Deploy to production (push) Successful in 28s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
44 lines
1.4 KiB
PHP
44 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Notifications\DatabaseNotification;
|
|
|
|
class NotificationController extends Controller
|
|
{
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$user = $request->user();
|
|
|
|
return response()->json([
|
|
'unread' => $user->unreadNotifications()->count(),
|
|
'items' => $user->notifications()->latest()->limit(20)->get()->map(fn (DatabaseNotification $notification): array => [
|
|
'id' => $notification->id,
|
|
'kind' => $notification->data['kind'] ?? 'info',
|
|
'title' => $notification->data['title'] ?? '',
|
|
'body' => $notification->data['body'] ?? '',
|
|
'level' => $notification->data['level'] ?? 'info',
|
|
'url' => $notification->data['url'] ?? null,
|
|
'read' => $notification->read_at !== null,
|
|
'time' => $notification->created_at->diffForHumans(),
|
|
])->all(),
|
|
]);
|
|
}
|
|
|
|
public function read(Request $request, string $notification): JsonResponse
|
|
{
|
|
$request->user()->notifications()->whereKey($notification)->first()?->markAsRead();
|
|
|
|
return response()->json(['ok' => true]);
|
|
}
|
|
|
|
public function readAll(Request $request): JsonResponse
|
|
{
|
|
$request->user()->unreadNotifications->markAsRead();
|
|
|
|
return response()->json(['ok' => true]);
|
|
}
|
|
}
|