- How do I replace Laravel’s default flash messages with SweetAlert2 notifications?
- First, install the package via `composer require php-flasher/flasher-sweetalert`. Replace `session()->flash()` calls with `sweetalert('message', 'success')` in your controllers. Ensure SweetAlert2 JS/CSS is loaded in your Blade layouts, and configure PHPFlasher in `config/flasher.php`. The package handles the conversion automatically.
- Does this package work with Laravel 10+ and PHP 8.2+?
- Yes, the package requires PHP 8.2+ and PHPFlasher ^2.5.1, which is fully compatible with Laravel 10+. It leverages modern PHP features like type hints and integrates seamlessly with Laravel’s session flash system, which remains unchanged.
- Can I use SweetAlert2 toasts instead of modals in my Laravel app?
- Absolutely. Pass the `toast: true` option to `sweetalert()` to render notifications as toasts. For example: `sweetalert('Updated!', 'success', ['toast' => true, 'timer' => 3000])`. You can also customize position, timer duration, and other SweetAlert2 toast properties.
- What if SweetAlert2 is already included in my project (e.g., via Laravel Breeze or Jetstream)?
- No conflicts—this package only extends PHPFlasher’s flash system. If SweetAlert2 is already loaded, you can safely use the package’s helper functions. For deduplication, configure Laravel Mix/Vite to alias or bundle SweetAlert2 once, ensuring no duplicate scripts are loaded.
- How do I test SweetAlert2 notifications in Laravel?
- Test PHP logic (e.g., `sweetalert()` calls) with unit tests using PHPUnit. For frontend behavior, use E2E tests (e.g., Laravel Dusk or Playwright) to verify toasts/modals render correctly. Mock SweetAlert2 in tests by overriding the global `Swal` object or using a testing library like `sweetalert2-test`.
- Will this package work with Redis or database session drivers?
- Yes, as long as your session driver supports Laravel’s flash messages (which Redis and database drivers do by default). The package relies on PHPFlasher’s session-based system, so ensure your `SESSION_DRIVER` in `.env` is configured correctly (e.g., `redis` or `database`).
- Can I customize the SweetAlert2 theme or icons for my Laravel app?
- Yes, you can pass custom options to `sweetalert()` to override SweetAlert2 defaults, such as icons, colors, or buttons. For deeper theming, extend the default Blade templates or include a custom SweetAlert2 CSS file. Example: `sweetalert('Error!', 'error', ['icon' => 'warning', 'customClass' => 'my-theme'])`.
- What’s the fallback for users with JavaScript disabled?
- The package doesn’t provide a built-in fallback, but you can combine it with traditional flash messages. Use `session()->flash()` alongside `sweetalert()` to ensure users see a basic alert if JS fails. For example: `session()->flash('error', 'JS disabled: ' . $message); sweetalert($message, 'error');` in your Blade view.
- How do I integrate this with Laravel Livewire or Inertia.js?
- For Livewire, trigger SweetAlert2 via JavaScript events (e.g., `@then` hooks) or use Livewire’s `emit()` to call `window.Swal.fire()`. For Inertia.js, include SweetAlert2 in your frontend assets and use the package’s PHP helpers as usual. Both frameworks work because the package decouples backend logic (PHP) from frontend rendering (JS).
- Are there alternatives to this package for Laravel notifications?
- Yes, alternatives include Laravel Notifications (for emails/push), Toast.js integrations (e.g., `laravel-toastr`), or custom Blade components with SweetAlert2. However, this package uniquely bridges Laravel’s native flash system with SweetAlert2, offering a zero-config upgrade path for teams already using `session()->flash()`. For modal-heavy apps, it’s the most seamless choice.