Код: Выделить всё
#[Computed]
public function notifications()
{
return auth()->user()->fresh()->unreadNotifications;
}
< /code>
Тогда в лезвии: < /p>
@forelse($this->notifications as $notification)
//Display notifications
@empty
No notifications
@endforelse
markAsRead('{{ $notification->id }}')
public function markAsRead(string $notificationId): void
{
auth()->user()->unreadNotifications()
->where('id', $notificationId)
->update(['read_at' => now()]);
}
< /code>
Все работает. Однако после того, как последнее уведомление было отмечено как чтение, текст «Нет уведомлений» не отображается. Сначала мне нужно обновление. Я пытаюсь понять, почему я не могу заставить это работать.public function markAsRead(string $notificationId): void
{
auth()->user()->unreadNotifications()
->where('id', $notificationId)
->update(['read_at' => now()]);
$this->dispatch('notificationsUpdated'); (or $this->dispatch('$refresh');)
}
< /code>
и в компоненте Volt: < /p>
protected $listeners = ['notificationsUpdated' => '$refresh'];
< /code>
также попробовал это вместо этого: < /p>
public array $notifications = [];
public function mount()
{
$this->loadNotifications();
}
public function loadNotifications()
{
$this->notifications = auth()->user()->unreadNotifications;
}
public function markAsRead(string $notificationId): void
{
auth()->user()->unreadNotifications()
->where('id', $notificationId)
->update(['read_at' => now()]);
$this->loadNotifications();
}
< /code>
Наконец, я попробовал: < /p>
public function loadNotifications()
{
$this->notifications = auth()->user()->fresh()->unreadNotifications;
}
< /code>
думает, что это будет перезагрузить свежий экземпляр пользователя или что -то в этом роде. < /p>
Но та же «проблема» продолжает происходить. Я не хочу показывать пользователю пустой блок, как только последнее уведомление было отмечено как чтение, но я хочу, чтобы он был непосредственно «без уведомлений», как указано в @empty of @forelse. Любое решение, конечно, бонус.
Подробнее здесь: https://stackoverflow.com/questions/797 ... ad-the-dom
Мобильная версия