41 lines
920 B
PHP
41 lines
920 B
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Notification;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class NotificationController extends Controller
|
|
{
|
|
use AuthorizesRequests;
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$user = $request->user();
|
|
|
|
$unread = $user->notifications()
|
|
->whereNull('read_at')
|
|
->latest()
|
|
->get();
|
|
|
|
if ($unread->isNotEmpty()) {
|
|
return response()->json($unread);
|
|
}
|
|
|
|
$recent = $user->notifications()
|
|
->latest()
|
|
->limit(10)
|
|
->get();
|
|
|
|
return response()->json($recent);
|
|
}
|
|
|
|
public function markRead(Request $request, Notification $notification)
|
|
{
|
|
$this->authorize('update', $notification);
|
|
$notification->markAsRead();
|
|
}
|
|
}
|