Scaffolded from the Ladill mini/events extraction pattern: multi-file transfers, retention controls, public landing pages, analytics, and Gitea deploy workflow. Co-authored-by: Cursor <cursoragent@cursor.com>
65 lines
1.8 KiB
PHP
65 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class NotificationController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
$notifications = $request->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]);
|
|
}
|
|
}
|