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.
First, you need to register the middleware in the web middleware group. This allows the middleware to be applied to web routes.
Open your app/Http/Kernel.php file.
Find the $middlewareGroups array. Within this array, locate the 'web' middleware group.
Add \RealRashid\SweetAlert\ToSweetAlert::class to the list of middleware in the 'web' group:
// app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
// Other middleware...
\RealRashid\SweetAlert\ToSweetAlert::class,
],
];
This registers the ToSweetAlert middleware for all routes in the web middleware group.
If you're using Laravel 11 specifically, you may need to adjust middleware usage as follows:
Navigate to your bootstrap/app.php file.
Locate where middleware is defined, typically inside the withMiddleware method call.
Modify the middleware setup to include ToSweetAlert::class specifically for the web middleware group:
// bootstrap/app.php
$middleware->web(append: [
\RealRashid\SweetAlert\ToSweetAlert::class,
]);
This ensures that the ToSweetAlert middleware is applied correctly within the context of Laravel 11's middleware handling.
Set the SWEET_ALERT_AUTO_DISPLAY_ERROR_MESSAGES .env value to true to activate the automatic displaying for the validation error messages.
Default is true.
Now within your controllers, just set your return message and send the proper message and proper type.
Errors Alert
public function store(Request $request)
{
//validation
$request->validate([
'title' => 'required|min:3',
'body' => 'required|min:3'
]);
$task = Task::create($request->all());
return redirect('tasks')->with('success', 'Task Created Successfully!');
// OR
return redirect('tasks')->withSuccess('Task Created Successfully!');
}
Error Alert
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|min:3',
'body' => 'required|min:3'
]);
if ($validator->fails()) {
return back()->with('errors', $validator->messages()->all()[0])->withInput();
}
$task = Task::create($request->all());
return redirect('tasks')->with('success', 'Task Created Successfully!');
// OR
return redirect('tasks')->withSuccess('Task Created Successfully!');
}
Success Alert
public function store(Request $request)
{
//validation
$task = Task::create($request->all());
return redirect('tasks')->with('success', 'Task Created Successfully!');
// OR
return redirect('tasks')->withSuccess('Task Created Successfully!');
}
return redirect('route')->with('type', 'message');
// OR
return redirect('route')->withType('message');
All available types : error success info warning question .
Error Toast
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|min:3',
'body' => 'required|min:3'
]);
if ($validator->fails()) {
return back()->with('toast_error', $validator->messages()->all()[0])->withInput();
}
$task = Task::create($request->all());
return redirect('tasks')->with('success', 'Task Created Successfully!');
// OR
return redirect('tasks')->withSuccess('Task Created Successfully!');
}
Success Toast
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|min:3',
'body' => 'required|min:3'
]);
if ($validator->fails()) {
return back()->with('toast_error', $validator->messages()->all()[0])->withInput();
}
$task = Task::create($request->all());
return redirect('tasks')->with('toast_success', 'Task Created Successfully!');
// OR
return redirect('tasks')->withToastSuccess('Task Created Successfully!');
}
return redirect('route')->with('toast_type', 'message');
// OR
return redirect('route')->withToastType('message');
All available types toast_error toast_success toast_info toast_warning toast_question.
!> You can not use helper methods with Middleware but you can set default values in config/sweetalert.php file! Recommend to use the .env keys.
SWEET_ALERT_MIDDLEWARE_AUTO_CLOSE=false
SWEET_ALERT_MIDDLEWARE_TOAST_POSITION='top-end'
SWEET_ALERT_MIDDLEWARE_TOAST_CLOSE_BUTTON=true
SWEET_ALERT_MIDDLEWARE_ALERT_CLOSE_TIME=5000
SWEET_ALERT_AUTO_DISPLAY_ERROR_MESSAGES=true
Positions 'top', 'top-start', 'top-end', 'center', 'center-start', 'center-end', 'bottom', 'bottom-start', or 'bottom-end'.
How can I help you explore Laravel packages today?