In the last blog , we explored creating multi-step forms in Livewire 3.0. Today, we’ll focus on implementing notifications and toast messages in Livewire 3.0. Notifications and toast messages are essential for providing real-time feedback to users, ensuring they stay informed about actions like form submissions, errors, or updates.
Notifications: Persistent or temporary messages displayed on the screen to inform users about the status of an action.
Toast Messages: Small, non-intrusive alerts that appear briefly and disappear automatically, often used for success or error messages.
Livewire 3.0 allows you to dispatch events for triggering notifications. Let’s set up a simple notification system.
Add a method to your Livewire component:
public function triggerNotification()
{
$this->dispatch('notify', [
'type' => 'success',
'message' => 'Your action was successful!'
]);
}
In the Blade template:
The dispatch
method emits a notify
event with the type and message.
The JavaScript notify
listener displays the message using alert
.
For better styling and animations, integrate a toast library like Toastify.
Install Toastify using npm
npm install toastify-js
Include the library in your main Blade layout:
Update your Blade template:
Run the following command:
php artisan make:livewire Notification
Update Notification.php
:
type = $type;
$this->message = $message;
$this->isVisible = true;
$this->dispatch('notify', [
'type' => $type,
'message' => $message
]);
// Auto-hide after 3 seconds
$this->dispatch('hideNotification', delay: 3000);
}
public function hideNotification()
{
$this->isVisible = false;
}
public function render()
{
return view('livewire.notification');
}
}
In notification.blade.php
:
@if ($isVisible)
{{ $message }}
@endif
You can use the Notification
component globally:
Include it in your layout:
Trigger notifications from any Livewire component:
$this->dispatch('showNotification', 'success', 'Action completed successfully!');
By following these steps, you can:
Provide instant feedback to users with notifications.
Use toast messages for non-intrusive alerts.
Build reusable components for notifications across your application.
Dispatching events for notifications in Livewire 3.0.
Adding styled toast messages with libraries like Toastify.
Building a reusable notification component.
Managing notifications dynamically in Livewire.
410 C, Jaina Tower-1, District Center, Janak Puri New Delhi-110058, India.
© Copyright 2025 Wontonee. All Right Reserved.