ldc-legaulois/app/Http/Controllers/Club/NotificationController.php
2026-09-25 16:25:31 +02:00

72 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers\Club;
use App\Http\Controllers\Controller;
use App\Http\Requests\Club\Notifications\DeleteNotificationsRequest;
use App\Models\Notification;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View as FacadesView;
class NotificationController extends Controller
{
/**
* Show index page
*/
public function index(): View
{
/** @var \App\Models\Customer $customerModel */
$customerModel = auth('club')->user();
$notificationModels = $customerModel->notifications()
->whereNotIn('id', $customerModel->readNotifications()->pluck('notification_id'))
->orderBy('created_at', 'DESC')
->paginate(10);
return view('club.pages.notifications.index', compact('notificationModels'));
}
/**
* Get notifications count
*/
public function notificationsCount(): JsonResponse
{
return response()->json(FacadesView::shared('notifications_count', 0));
}
/**
* Delete notification from list
*/
public function destroy(Notification $notification): JsonResponse
{
/** @var \App\Models\Customer $customer */
$customer = Auth::guard('club')->user();
$customer->readNotifications()->syncWithoutDetaching($notification->id);
session()->flash('success', __('Notification supprimée.'));
return response()->json(['success' => true]);
}
/**
* Delete notification from list
*/
public function destroyMultiple(DeleteNotificationsRequest $request): JsonResponse
{
/** @var \App\Models\Customer $customer */
$customer = Auth::guard('club')->user();
$customer->readNotifications()->syncWithoutDetaching($request->validated('ids'));
$count = count($request->validated(('ids')));
session()->flash('success', trans_choice(
'{1} Notification supprimée.|[2,*] :count notifications supprimées.',
$count,
['count' => $count]
));
return response()->json(['success' => true]);
}
}