A simple demo application showing how to integrate SweetAlert2 with Laravel and Inertia React using the sweetalert2/laravel package.
- 🎉 Beautiful alerts and toasts with SweetAlert2
- ⚡ Seamless Laravel + Inertia React integration
- 🔄 Server-side alert triggering with client-side display
- 📦 Simple todo app demonstrating the integration
- Clone the repository:
git clone https://github.com/sweetalert2/sweetalert2-laravel-inertia-react-demo.git
cd sweetalert2-laravel-inertia-react-demo- Install dependencies and set up the application:
composer setup- Run the development server:
composer devThe application will be available at http://localhost:8000.
Add the SweetAlert2 template to your Blade layout (resources/views/app.blade.php):
@include('sweetalert2::index')Share the SweetAlert2 session data in HandleInertiaRequests.php:
use SweetAlert2\Laravel\Swal;
public function share(Request $request): array
{
return array_merge(parent::share($request), [
'flash' => [
Swal::SESSION_KEY => fn () => $request->session()->pull(Swal::SESSION_KEY),
],
]);
}Use the Swal facade anywhere in your Laravel controllers or routes:
use SweetAlert2\Laravel\Swal;
// Show a success toast after saving
Swal::toastSuccess([
'title' => 'Saved!',
'position' => 'top-end',
'showConfirmButton' => false,
'timer' => 3000,
'timerProgressBar' => true,
]);
return redirect('/');// Alerts with icons
Swal::success(['title' => 'Success!']);
Swal::error(['title' => 'Error!']);
Swal::warning(['title' => 'Warning!']);
Swal::info(['title' => 'Info!']);
Swal::question(['title' => 'Question?']);
// Toast notifications
Swal::toast(['title' => 'Toast']);
Swal::toastSuccess(['title' => 'Success!']);
Swal::toastError(['title' => 'Error!']);
Swal::toastWarning(['title' => 'Warning!']);
Swal::toastInfo(['title' => 'Info!']);
Swal::toastQuestion(['title' => 'Question?']);
// Full configuration
Swal::fire([
'title' => 'Custom Alert',
'text' => 'With custom options',
'icon' => 'success',
'confirmButtonText' => 'OK',
// ...any SweetAlert2 option
]);See the SweetAlert2 documentation for all available options.
- Laravel 12
- Inertia.js 2
- React 19
- Tailwind CSS 4
- SweetAlert2
MIT