Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Livewire

Building Notifications with Livewire v4

Create toast notifications system.

1 February 20261 min readLivewireاقرأ بالعربية

On this page

  1. Toast Component
  2. Trigger

Build a notification system.

Toast Component

php
@php
new class extends Livewire\Component {
    public array $notifications = [];
    
    #[On('notify')]
    public function add(string $message, string $type = 'info'): void
    {
        $this->notifications[] = ['id' => uniqid(), 'message' => $message, 'type' => $type];
    }
    
    public function remove(string $id): void
    {
        $this->notifications = array_filter($this->notifications, fn($n) => $n['id'] !== $id);
    }
}
@endphp

Trigger

php
$this->dispatch('notify', message: 'Saved!', type: 'success');
All articles

Keep reading

  • Authorization in Livewire v4 Components1 February 20261 min read
  • Blaze Pre-Compiler: Massive Performance Gains in Livewire v41 February 20261 min read
  • Building a Bookmark Feature with Livewire v41 February 20261 min read