A demonstration of SweetAlert2-Laravel integration with Livewire.
composer install
bun install
cp .env.example .env
php artisan key:generate
php artisan migrateInstall the package:
composer require sweetalert2/laravelInclude the SweetAlert2 template in your layout file (e.g., resources/views/layouts/app.blade.php):
@include('sweetalert2::index')Add the WithSweetAlert trait to your Livewire component:
use Livewire\Component;
use SweetAlert2\Laravel\Traits\WithSweetAlert;
class Counter extends Component
{
use WithSweetAlert;
public function increment(): void
{
$this->swalSuccess([
'title' => 'Success!',
'text' => 'Your action was successful.',
'toast' => true,
'showConfirmButton' => false,
'position' => 'top-end',
'timer' => 1500,
'timerProgressBar' => true,
'didOpen' => '(toast) => {
toast.onmouseenter = Swal.stopTimer;
toast.onmouseleave = Swal.resumeTimer;
}',
]);
}
}composer run devVisit the application and interact with the Livewire counter component to see SweetAlert2 in action.
php artisan testMIT