Download the PHP package sweetalert2/laravel without Composer
On this page you can find all versions of the php package sweetalert2/laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sweetalert2/laravel
More information about sweetalert2/laravel
Files in sweetalert2/laravel
Package laravel
Short Description Official SweetAlert2 integration for Laravel framework
License MIT
Informations about the package laravel
Installation
Include the SweetAlert2 template in your layout file (usually resources/views/layouts/app.blade.php
):
Usage
Laravel Controllers, Middleware, Views etc.
You can now call Swal::fire()
or any of the available helper methods anywhere in your Laravel application (controllers, middleware, etc.) to show a SweetAlert2 alert:
The full list of options can be found in the SweetAlert2 documentation.
Helpers
Available Laravel helper methods:
Livewire Components
You can now call $this->swalFire
or any of the available helper methods in your Livewire component to show realtime popups and toasts:
LivewireExample.php
The full list of options can be found in the SweetAlert2 documentation.
livewire-example.blade.php
Helpers
Available Livewire helper methods:
FAQ
1. How is this different to the realrashid/sweet-alert package?
The realrashid/sweet-alert
package is too opinionated and too complex: facade, midddleware, adding vendor files, whatnot 🤯. And all that with 0 tests.
This package is simple, straightforward, and unopinionated. Its API is aimed to be as close as possible to the original sweetalert2.
It simply provides a way to use SweetAlert2 in your Laravel or Livewire application without touching JS or CSS files.
2. How does it work?
Depending on whether you use the Swal class or the WithSweetAlert trait, within either a Laravel only or Livewire context, the behaviour is slightly different.
Laravel controllers, middleware, views etc
- The
Swal::fire()
or$this->swalFire()
method will pass the options to the flashed session. - The blade partial template will check if there is any flashed session data and will render the SweetAlert2 popup.
Livewire components
Realtime
- The
$this->swalFire()
method will dispatch a Livewire event with the options to the browser window within the current request. - The blade partial template will listen for the Livewire event and will render the SweetAlert2 popup.
This works on the first request and subsequent Livewire update requests.
First request or after redirect
- The
Swal::fire()
method will pass the options to the flashed session. - The blade partial template will show a popup on the first request.
This works only on the first request, not on Livewire update requests.
This is ideal for showing messages after redirecting the user from a Livewire component, for example if they lack permissions to view a page:
3. How is the SweetAlert2 JavaScript library loaded?
This package uses a smart loading strategy for the SweetAlert2 library:
-
Check for existing SweetAlert2: If
window.Swal
is already available, it will use the existing import. - Dynamic CDN loading: If SweetAlert2 is not loaded, it will dynamically import it from the official CDN (
https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.esm.all.min.js
).
4. What are the limitations?
SweetAlert2 is a JavaScript package and some of its options are JS callbacks. It's not possible to use them in the Swal::fire()
or $this->swalFire()
methods.
If you need to use JS callbacks, you have to go to JS and use the SweetAlert2 API directly.