File: /home/xedaptot/hi.naniguide.com/app/Http/Controllers/Admin/NotificationController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class NotificationController extends Controller
{
public function feed(Request $request): JsonResponse
{
$user = auth('admin')->user();
if (!$user) {
return response()->json(['message' => 'Unauthenticated'], 401);
}
$notifications = $user->notifications()
->latest()
->limit(10)
->get()
->map(function ($notification) {
return [
'id' => $notification->id,
'is_read' => (bool) $notification->read_at,
'read_at' => $notification->read_at?->toIso8601String(),
'created_at' => $notification->created_at?->toIso8601String(),
'created_at_human' => $notification->created_at?->diffForHumans(),
'type' => $notification->data['type'] ?? null,
'title' => $notification->data['title'] ?? 'Notification',
'message' => $notification->data['message'] ?? null,
'data' => $notification->data,
];
})
->values();
return response()->json([
'unread_count' => $user->unreadNotifications()->count(),
'notifications' => $notifications,
]);
}
public function markAllRead(Request $request): JsonResponse
{
$user = auth('admin')->user();
if (!$user) {
return response()->json(['message' => 'Unauthenticated'], 401);
}
$user->unreadNotifications->markAsRead();
return response()->json([
'unread_count' => 0,
]);
}
}