Installation:
composer require atomcoder/laravel-toasty
Publish the config (optional):
php artisan vendor:publish --provider="Atomcoder\Toasty\ToastyServiceProvider"
First Toast in Blade:
@toasty('Success!', 'Your action was completed.', 'success')
Place this in your Blade layout or view where toasts should appear.
First Toast in Livewire:
use Atomcoder\Toasty\Facades\Toasty;
public function mount() {
Toasty::success('Success!', 'Your action was completed.');
}
First Toast via Redirect:
return redirect()->route('target')->withToasty('success', 'Success!', 'Your action was completed.');
@toasty and @toastyStack in the README.Atomcoder\Toasty\Facades\Toasty methods in the API docs.Global Toasts:
Add @toastyStack to your main layout (e.g., resources/views/layouts/app.blade.php) to render all queued toasts.
@toastyStack
Conditional Toasts: Use Blade conditionals to show toasts only when needed:
@if(session('toasty.success'))
@toasty('Success!', session('toasty.success.message'), 'success')
@endif
Dynamic Content: Pass variables from controllers to Blade:
return view('dashboard')->withToasty('info', 'Welcome back!', 'You are now logged in.');
Component-Level Toasts:
Use the Toasty facade in Livewire components:
public function save() {
try {
// Save logic...
Toasty::success('Saved!', 'Your changes were saved successfully.');
} catch (\Exception $e) {
Toasty::error('Error!', $e->getMessage());
}
}
Stacking Toasts: Queue multiple toasts in a row:
public function process() {
Toasty::info('Processing...', 'This may take a moment.');
// Simulate async work
sleep(1);
Toasty::success('Done!', 'Processing completed.');
}
Livewire Component Wrapper:
Extend the ToastyLivewire component for reusable toast handling:
use Atomcoder\Toasty\Livewire\ToastyLivewire;
class MyComponent extends ToastyLivewire {
// Custom logic...
}
Flash Toasts:
Use withToasty in redirects to persist toasts across requests:
return redirect()->route('dashboard')->withToasty('success', 'Profile updated!', 'Your profile has been saved.');
Session-Based Toasts:
Access toasts in Blade via session('toasty'):
@if(session('toasty.success'))
@toasty('Success', session('toasty.success.message'), 'success')
@endif
Manual Triggering:
Use the toasty() global function in Alpine:
<button @click="toasty('info', 'Hello!', 'This is a toast from Alpine.')">
Show Toast
</button>
Dynamic Alpine Events: Trigger toasts from Alpine event handlers:
document.addEventListener('alpine:init', () => {
Alpine.data('myComponent', () => ({
showToast() {
toasty('success', 'Action completed!', 'Your request was processed.');
}
}));
});
Vanilla JS:
Call the global toasty() function:
toasty('warning', 'Warning!', 'This action cannot be undone.');
AJAX Responses: Handle toasts in AJAX success/error callbacks:
$.ajax({
url: '/update',
success: function() {
toasty('success', 'Updated!', 'Your data has been saved.');
},
error: function(xhr) {
toasty('error', 'Error!', xhr.responseJSON.message || 'An error occurred.');
}
});
Toast Not Rendering:
@toastyStack is included in your Blade layout.toasty.js).toasty global is available in the browser console.Livewire Toasts Not Showing:
ToastyLivewire component is registered in your Livewire stack.wire:ignore on the toast container if needed:
<div wire:ignore>
@toastyStack
</div>
Session Toasts Disappearing:
session('toasty'). Ensure your session driver is configured correctly.Namespace Conflicts:
toasty namespace. Avoid naming custom functions or variables toasty to prevent conflicts.Check the Queue: Dump the toast queue in Blade to verify toasts are being set:
@dump(session('toasty.queue', []))
Inspect JavaScript:
Open browser dev tools (F12) and check the Console for errors. Look for:
toasty is not defined
This indicates the script failed to load.
Clear Old Toasts: If toasts persist unexpectedly, clear the session manually:
session()->forget('toasty.queue');
session()->forget('toasty.success');
session()->forget('toasty.error');
// ... repeat for other types
Custom Toast Types:
Extend the default types (success, error, info, warning) by publishing the config and adding your own:
'types' => [
'success' => ['icon' => '✅', 'class' => 'bg-green-500'],
'error' => ['icon' => '❌', 'class' => 'bg-red-500'],
'custom' => ['icon' => '🔧', 'class' => 'bg-blue-500'],
],
Auto-Dismissal:
Disable auto-dismissal globally in config/toasty.php:
'auto_dismiss' => false,
Or per-toast:
Toasty::info('Persistent Toast', 'This will not auto-dismiss.', ['autoDismiss' => false]);
Positioning:
Change the toast position (default: top-right) in the config:
'position' => 'bottom-right',
Custom Toast Views: Override the default toast Blade view by publishing the assets:
php artisan vendor:publish --tag=toasty-views
Then modify resources/views/vendor/toasty/toast.blade.php.
Alpine Integration:
Extend the Alpine toasty() function by adding custom logic to the global script:
window.toasty = function(type, title, message, options = {}) {
// Custom logic here
Toasty.show(type, title, message, options);
};
Livewire Events: Listen for toast events in Livewire:
protected $listeners = ['toast:show' => 'handleToast'];
public function handleToast($event) {
// Handle custom toast logic
}
Queue Management: Manually manage the toast queue in Blade:
@php
$toasts = session('toasty.queue', []);
session()->forget('toasty.queue');
@endphp
@foreach($toasts as $toast)
@toasty($toast['title'], $toast['message'], $
How can I help you explore Laravel packages today?