devrabiul/laravel-toaster-magic
Dependency-free toast notifications for Laravel with Livewire v3/v4 support. Drop-in, customizable toasts with multiple modern themes, RTL + dark mode, XSS-safe links, and no need for jQuery, Bootstrap, or Tailwind.
Installation:
composer require devrabiul/laravel-toaster-magic
php artisan vendor:publish --provider="Devrabiul\ToastMagic\ToastMagicServiceProvider"
(Assets auto-publish on first page load.)
Add to Blade:
<head>
{!! ToastMagic::styles() !!}
</head>
<body>
{!! ToastMagic::scripts() !!}
</body>
First Toast:
use Devrabiul\ToastMagic\Facades\ToastMagic;
ToastMagic::success('Success!', 'Your action was completed.');
config/laravel-toaster-magic.php (position, theme, Livewire settings).Devrabiul\ToastMagic\Facades\ToastMagic (core API).this->dispatch('toastMagic', [...]) (event-based usage).Trigger a success toast after a form submission in a controller:
public function store(Request $request) {
$request->validate([...]);
ToastMagic::success('Saved!', 'Your data is now stored.');
return back();
}
Controller Integration:
// Static method (quick)
ToastMagic::warning('Warning!', 'Check your inputs.', ['timeOut' => 8000]);
// Fluent method (customizable)
ToastMagic::dispatch()
->error('Failed', 'Something went wrong.')
->withOptions(['theme' => 'neon', 'positionClass' => 'toast-bottom-end']);
Livewire Integration:
// Component method
public function createUser() {
$this->validate([...]);
$this->dispatch('toastMagic', [
'status' => 'success',
'title' => 'User Created',
'message' => 'Profile saved.',
'options' => ['customBtnText' => 'View', 'customBtnLink' => route('profile')],
]);
}
JavaScript Integration:
const toast = new ToastMagic();
toast.info('Update Available', 'New version ready.', false, 'Download', '/update');
ToastMagic::success('...', '...', ['theme' => 'glassmorphism']);
positionClass for dynamic layouts (e.g., mobile vs. desktop):
if (request()->isMobile()) {
ToastMagic::setOption('positionClass', 'toast-bottom-start');
}
protected $listeners = ['toastMagic' => 'handleToast'];
public function handleToast($event) {
$this->dispatchBrowserEvent('toastMagic', $event);
}
if ($errors->any()) {
foreach ($errors->all() as $error) {
ToastMagic::error('Validation Error', $error);
}
}
XSS Risks:
customBtnLink) are sanitized to prevent javascript: injection.http://, https://, /, or # URLs are allowed. Invalid links default to #.Livewire Version Mismatch:
livewire_version in config matches your project.$this->dispatch('toastMagic', ['status' => 'info']);
Caching Issues:
php artisan migrate may fail if CACHE_DRIVER=database and the cache table doesn’t exist.Message Truncation:
rtrim() was corrupting messages with <br> tags (e.g., "error" → "err").preg_replace('/(<br>)+$/', '', $string) in all toast methods.Toast Not Showing?:
ToastMagic::styles() and ToastMagic::scripts() are in <head>/</body>.Livewire Not Working:
livewire_enabled: true in config.v3 or v4) matches your project.protected $listeners = ['toastMagic' => 'showToast'];
public function showToast($event) { dd($event); }
default, material, or neumorphism themes.theme="dark" on <body> (not a config option).Custom Themes:
php artisan vendor:publish --tag=laravel-toaster-magic-assets --force
resources/views/vendor/toast-magic/themes/ and recompile assets.Event Listeners:
Livewire::listen('toastMagic', function ($event) {
// Log or modify events
});
Middleware:
public function handle($request, Closure $next) {
if (!$request->wantsJson()) {
app()->singleton(ToastMagic::class, fn() => new class {
public function __call($method, $args) { return null; }
});
}
return $next($request);
}
Testing:
$this->mock(ToastMagic::class)->shouldReceive('success')->once();
How can I help you explore Laravel packages today?