Installation:
composer require ascsoftw/livewire-toast
Add to Layout:
Place @livewire('livewire-toast') in your main layout file (e.g., resources/views/layouts/app.blade.php), ideally near the closing </body> tag for proper positioning.
First Toast: Emit a test toast from a Livewire component:
$this->emitTo('livewire-toast', 'show', 'Hello, Toast!');
vendor/ascsoftw/livewire-toast/src/LivewireToast.php (core logic)vendor/ascsoftw/livewire-toast/resources/views/livewire/livewire-toast.blade.php (customization entry point)Livewire Component Emission:
Use $this->emitTo() in Livewire methods to trigger toasts dynamically:
public function saveProject()
{
if ($this->validate()) {
$this->emitTo('livewire-toast', 'showSuccess', 'Project saved!');
// ...
} else {
$this->emitTo('livewire-toast', 'showError', 'Validation failed.');
}
}
Session Flash Integration: Leverage Laravel’s session for toasts that persist across requests (e.g., after form submissions):
public function store(Request $request)
{
$validated = $request->validate([...]);
Project::create($validated);
return redirect()->back()->with('livewire-toast', [
'type' => 'success',
'message' => 'Project created!'
]);
}
Conditional Toasts: Combine with Livewire’s reactivity for dynamic messages:
public $isSuccess = false;
public function attemptAction()
{
$this->isSuccess = true; // Simulate success
$this->emitTo('livewire-toast', 'show', [
'type' => $this->isSuccess ? 'success' : 'error',
'message' => $this->isSuccess ? 'Action succeeded!' : 'Action failed.'
]);
}
View-Based Emission:
For non-Livewire Blade views, use the $emitTo helper (if registered globally):
@if(session('livewire-toast'))
@php
$emitTo('livewire-toast', 'show', session('livewire-toast'));
@endphp
@endif
$this->emitTo('livewire-toast', 'show', [
'message' => 'Custom duration',
'duration' => 10000 // 10 seconds
]);
bg-blue-500 for custom colors).role="alert" in the Blade template).Missing Component:
Class 'LivewireToast' not found.@livewire('livewire-toast') is placed in a layout file loaded on every request (e.g., app.blade.php).Session Flash Not Triggering:
session()->flash() don’t appear.'livewire-toast' (case-sensitive). Check if middleware (e.g., ShareErrorsFromSession) is interfering.Duplicate Toasts:
$this->emitTo('livewire-toast', 'show', ['id' => uniqid(), 'message' => '...']);
Styling Overrides:
!important in global styles). Inspect the rendered HTML for applied classes.AlpineJS Conflicts:
wire:click or wire:model events to verify emissions:
<button wire:click="testToast">Test Toast</button>
public function testToast()
{
$this->emitTo('livewire-toast', 'show', 'Debug message');
}
livewire-toast@show) in the browser’s DevTools.show() method (override the package’s component if needed).duration: 5000). To customize:
livewire-toast:publish).duration in the emit array).success, error, warning, info. Passing unsupported types (e.g., 'custom') may render as default styling.danger):
@if($type === 'danger')
<div class="bg-red-500 text-white">...</div>
@endif
<div x-data="{ paused: false }" @mouseenter="paused = true" @mouseleave="paused = false">
<!-- Toast content -->
</div>
// In LivewireToast.php
$this->dispatchBrowserEvent('pause-toast', ['paused' => true]);
Queue::push(function () {
$this->emitTo('livewire-toast', 'show', 'Queued toast');
});
__() for translation support:
$this->emitTo('livewire-toast', 'show', __('toast.project_saved'));
How can I help you explore Laravel packages today?