nimuli/app/Livewire/Components/NotificationsBell.php

42 lines
902 B
PHP

<?php
namespace App\Livewire\Components;
use Livewire\Component;
class NotificationsBell extends Component
{
public bool $open = false;
public function getUnreadCountProperty(): int
{
return auth()->user()?->unreadNotifications()->count() ?? 0;
}
public function getNotificationsProperty()
{
return auth()->user()?->notifications()->latest()->take(10)->get() ?? collect();
}
public function toggle(): void
{
$this->open = ! $this->open;
}
public function markRead(string $id): void
{
auth()->user()?->notifications()->where('id', $id)->first()?->markAsRead();
}
public function markAllRead(): void
{
auth()->user()?->unreadNotifications->markAsRead();
$this->open = false;
}
public function render()
{
return view('livewire.components.notifications-bell');
}
}