47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\UserNotification;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class NotificationController extends Controller
|
|
{
|
|
public function index(): \Illuminate\View\View
|
|
{
|
|
$notifications = UserNotification::where('user_id', Auth::id())
|
|
->orderByDesc('created_at')
|
|
->paginate(20);
|
|
|
|
return view('notifications.index', compact('notifications'));
|
|
}
|
|
|
|
public function markAsRead(UserNotification $notification): \Illuminate\Http\RedirectResponse
|
|
{
|
|
$this->authorizeNotification($notification);
|
|
|
|
$notification->markAsRead();
|
|
|
|
return back()->with('success', __('Notification marked as read.'));
|
|
}
|
|
|
|
public function markAllAsRead(): \Illuminate\Http\RedirectResponse
|
|
{
|
|
UserNotification::where('user_id', Auth::id())
|
|
->where('is_read', false)
|
|
->update(['is_read' => true, 'read_at' => now()]);
|
|
|
|
return back()->with('success', __('All notifications marked as read.'));
|
|
}
|
|
|
|
private function authorizeNotification(UserNotification $notification): void
|
|
{
|
|
if ($notification->user_id !== Auth::id()) {
|
|
abort(403);
|
|
}
|
|
}
|
|
}
|