Download the PHP package eru/sweet-alert without Composer
On this page you can find all versions of the php package eru/sweet-alert. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package sweet-alert
Easy Sweet Alert Messages for Laravel
Installation
Require the package using Composer.
If using laravel < 5.5 include the service provider and alias within config/app.php
.
Installing Frontend Dependency
This package works only by using the BEAUTIFUL REPLACEMENT FOR JAVASCRIPT'S "ALERT".
Using a CDN
Using Laravel Mix
Install using Yarn
Install using NPM
Require sweetalert within your resources/js/bootstrap.js
file.
Then make sure to include your scripts in your blade layout. Remove the defer
attribute if your script tag contains it, defer
will delay the execution of the script which will cause an error as the sweet::alert
blade template is rendered first by the browser as html.
Finally compile your assets with Mix
Usage
Using the Facade
First import the SweetAlert facade in your controller.
Within your controllers, before you perform a redirect...
Here are some examples on how you can use the facade:
Using the helper function
alert($message = null, $title = '')
In addition to the previous listed methods you can also just use the helper function without specifying any message type. Doing so is similar to:
alert()->message('Message', 'Optional Title')
Like with the Facade we can use the helper with the same methods:
Within your controllers, before you perform a redirect...
For a general information alert, just do: alert('Some message');
(same as alert()->message('Some message');
).
Using the Middleware
Middleware Groups
First register the middleware in web middleware groups by simply adding the middleware class eru\SweetAlert\ConvertMessagesIntoSweetAlert::class
into the \$middlewareGroups of your app/Http/Kernel.php class:
Make sure you register the middleware within the 'web' group only.
Route Middleware
Or if you would like to assign the middleware to specific routes only, you should add the middleware to $routeMiddleware
in app/Http/Kernel.php
file:
Next step: within your controllers, set your return message (using with()
) and send the proper message and proper type.
or
NOTE: When using the middleware it will make an alert to display if it detects any of the following keys flashed into the session:
error
,success
,warning
,info
,message
,basic
.
Final Considerations
By default, all alerts will dismiss after a sensible default number of seconds.
But not to worry, if you need to specify a different time you can:
Also, if you need the alert to be persistent on the page until the user dismiss it by pressing the alert confirmation button:
You can render html in your message with the html() method like this:
Customize
Config
If you need to customize the default configuration options for this package just export the configuration file:
A sweet-alert.php
configuration file will be published to your config
directory. By now, the only configuration that can be changed is the timer for all autoclose alerts.
View
If you need to customize the included alert message view, run:
The package view is located in the resources/views/vendor/sweet/
directory.
You can customize this view to fit your needs.
Configuration Options
You have access to the following configuration options to build a custom view:
Please check the CONFIGURATION section in the website for all other options available.
Default View
The sweet_alert.alert
session key contains a JSON configuration object to pass it directly to Sweet Alert.
Note that {!! !!}
are used to output the json configuration object unescaped, it will not work with {{ }}
escaped output tags.
Custom View
This is an example of how you can customize your view to fit your needs:
Note that you must use ""
(double quotes) to wrap the values except for the timer option.
Tests
To run the included test suite:
Demo
License
Sweet Alert for Laravel is open-sourced software licensed under the MIT license.