realrashid/sweet-alert
Laravel package to integrate SweetAlert2 popups: stylish alerts, confirmations, toasts, and notifications. Trigger from controllers or views with simple helpers and session flashes, customize options, and improve UX with minimal setup.
Installation:
composer require realrashid/sweet-alert
Publish assets (optional, for custom themes):
php artisan vendor:publish --provider="RealRashid\SweetAlert\SweetAlertServiceProvider"
First Usage (in a controller):
use RealRashid\SweetAlert\Facades\Alert;
public function showSuccess()
{
Alert::success('Success!', 'Your action was completed successfully.');
}
Blade Integration:
Add @include('sweetalert::alert') to your layout file (e.g., resources/views/layouts/app.blade.php).
// In your controller
public function store(Request $request)
{
try {
// Process form data
Alert::success('Submitted!', 'Your form was submitted successfully.');
} catch (\Exception $e) {
Alert::error('Error!', 'Failed to submit: ' . $e->getMessage());
}
}
Alert::success('Title', 'Message');
alert()->warning('Title', 'Message');
public function updateProfile(Request $request)
{
$validated = $request->validate([...]);
Alert::success('Profile Updated', "Your profile has been updated with {$request->name}.")
->timerProgressBar(); // Show progress bar
}
public function deletePost($id)
{
Alert::question('Delete Post?', 'Are you sure you want to delete this post?', function () {
Post::find($id)->delete();
return redirect()->back()->with('success', 'Post deleted!');
});
}
Alert::alert('Custom Alert', 'Message')
->position('top-center')
->timer(3000)
->showConfirmButton()
->focusConfirm();
// app/Http/Middleware/SetTheme.php
public function handle($request, Closure $next)
{
if (auth()->check() && auth()->user()->prefers_dark_mode) {
config(['sweetalert.theme' => 'dark']);
}
return $next($request);
}
// In a controller
toast('Saved!', 'success');
// Or with custom position
toast('Updated!', 'info')->position('top-left');
Missing @include('sweetalert::alert'):
Facade Not Found:
SweetAlertServiceProvider is registered in config/app.php under providers.Theme Not Applying:
.env has SWEET_ALERT_THEME=your-theme or use config(['sweetalert.theme' => 'dark']) in a service provider.JavaScript Errors:
config/sweetalert.php is correct.Laravel 12+ Facade Issues:
php artisan config:clear
Check Console for Errors:
F12) to see if SweetAlert2 scripts load without errors.Inspect Rendered HTML:
<script> tags with sweetalert2 in the footer. If missing, the package isn’t publishing assets.Verify Config:
// Dump current config
dd(config('sweetalert'));
Test Toast Position:
bottom-right. Override globally in config/sweetalert.php or per-call:
toast('Message', 'success')->position('top-center');
Custom Themes:
resources/views/vendor/sweetalert/themes/your-theme.blade.php.Middleware for Global Alerts:
// app/Http/Middleware/ShowValidationErrors.php
public function handle($request, Closure $next)
{
$response = $next($request);
if ($response->getStatusCode() === 422) {
Alert::error('Validation Error', 'Please fix the errors below.');
}
return $response;
}
Dynamic Icons:
Alert::alert('Title', 'Message', 'custom')
->iconHtml('<i class="fas fa-rocket"></i>');
Localization:
resources/lang/vendor/sweetalert/.Conditional Alerts:
if ($user->isAdmin()) {
Alert::success('Admin Action', 'Only admins can perform this action.');
}
confirmDelete for CRUD:
Alert::confirmDelete('Delete User?', 'user.id', 'users', function () {
return redirect()->route('users.index');
});
Alert::alert('Title', 'Message')
->timer(5000)
->showCloseButton()
->reverseButtons();
SWEET_ALERT_MIDDLEWARE=false in .env to bypass middleware-triggered alerts.html() for Rich Content:
Alert::html('HTML Alert', '<strong>Bold</strong> and <a href="#">links</a> supported.', 'info');
How can I help you explore Laravel packages today?