Installation:
composer require php-flasher/flasher-sweetalert
Ensure php-flasher/core (v2.5.1+) is installed as a dependency.
Publish Configuration (if needed):
php artisan vendor:publish --provider="PHPFlasher\Flasher\FlasherServiceProvider" --tag="config"
Modify config/flasher.php to set default SweetAlert options (e.g., default_timer, default_toast).
First Use Case: Replace a basic flash message in a controller:
use PHPFlasher\Flasher\Facades\Flasher;
public function updateProfile(Request $request) {
$request->user()->update($request->validated());
Flasher::success('Profile updated successfully!')->toast()->timer(3000);
return redirect()->back();
}
Controller Integration:
Use Flasher facade for type-safe notifications:
Flasher::error('Validation failed', ['timer' => 5000])->toast();
Flasher::warning('Action requires confirmation')->modal();
Blade Rendering:
Add this to your resources/views/layouts/app.blade.php:
@flasher
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
Dynamic Options: Chain methods for clarity:
Flasher::success('Data saved')
->title('Success!')
->position('top-end')
->timer(2000)
->toast();
Conditional Flashing:
if ($user->isAdmin()) {
Flasher::info('Admin actions logged')->timer(4000);
}
Custom Types: Extend the package via service providers:
Flasher::extend('custom', function ($message, $options) {
return Flasher::make('custom', $message, $options)
->icon('fas fa-robot')
->timer(3000);
});
API Responses:
Combine with Laravel’s response()->json():
return response()->json([
'status' => 'success',
'data' => $data,
])->withFlasher(Flasher::success('Operation completed'));
Queue Delayed Flashes: Use Laravel queues to defer notifications:
Flasher::queue(Flasher::error('Payment failed'), now()->addMinutes(5));
Missing SweetAlert2 JS:
Type Mismatch Errors:
TypeError when passing invalid options (e.g., timer as a string).Flasher::success('Message')->timer(3000); // Correct
Flasher::success('Message')->timer('3000'); // Throws error
Configuration Overrides:
flasher.php may override per-flash settings.Flasher::make()->overrideConfig(['timer' => 0]) to bypass defaults.Modal Confirmation Handling:
preConfirm or attach JS handlers:
document.addEventListener('flasher:confirm', (e) => {
if (e.detail.type === 'success') {
window.location.href = '/dashboard';
}
});
Inspect Rendered HTML:
Check if the @flasher directive outputs the correct data structure. Use:
dd(Flasher::getMessages());
Disable Auto-Close:
Set timer: 0 in options to debug modal behavior without auto-dismissal.
Clear Flashes: Reset all flashes in a route:
Flasher::clear();
Custom Icons: Override default icons via config:
'icons' => [
'success' => 'fas fa-check-circle',
'error' => 'fas fa-exclamation-triangle',
],
Localization: Extend the package to support multi-language messages:
Flasher::setLocale('es');
Flasher::error(__('messages.error.default'));
Dark Mode:
Add a darkMode option to options:
Flasher::success('Message')->darkMode(true);
Then extend the JS template to include:
SweetAlert2.extend({
darkMode: false,
template: `
<div class="swal2-popup ${this.darkMode ? 'swal2-dark' : ''}">
<!-- content -->
</div>
`
});
How can I help you explore Laravel packages today?