Laravel SweetAlert

News

April 16th, 2019

Laravel SweetAlert

Laravel SweetAlert 2 is a Laravel package by Rashid Ali that integrates the popular JavaScript SweetAlert 2 with Laravel. Using the package’s Alert facade or alert() helper, you can queue up SweetAlert 2 alerts on the frontend from your controller:

use RealRashid\SweetAlert\Facades\Alert;
 
Alert::success('Success Title', 'Success Message');
Alert::info('Info Title', 'Info Message');
Alert::warning('Warning Title', 'Warning Message');
Alert::error('Error Title', 'Error Message');
 
// Using the helper
alert()->success('Title','Lorem Lorem Lorem');
alert()->info('Title','Lorem Lorem Lorem');
alert()->warning('Title','Lorem Lorem Lorem');
alert()->error('Title','Lorem Lorem Lorem');

You can also configure the package’s ToSweetAlert middleware to handle SweetAlert 2 messages during a middleware handler:

public function someFunction(Request $request)
{
return redirect('login')->with('success', 'Login Successfully!');
}

This package provides some of the boilerplate around integrating SweetAlert in an opinionated way. However, you might want to integrate SweetAlert 2 on your own if you need customizations beyond the scope of what this package’s API provides. You should check out the documentation to see what methods are available.

You can learn more about this package and check out the source code on GitHub at realrashid/sweet-alert.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.