📅  最后修改于: 2023-12-03 15:20:24.615000             🧑  作者: Mango
SweetAlert Laravel is a PHP package that integrates the SweetAlert library into Laravel applications. SweetAlert is a beautiful, responsive, customizable, and accessible (WAI-ARIA) replacement for JavaScript's alert and confirmation dialogs.
composer require realrashid/sweet-alert
config/app.php
file:'providers' => [
// other providers
RealRashid\SweetAlert\SweetAlertServiceProvider::class,
],
aliases
section of your config/app.php
file:'aliases' => [
// other aliases
'Alert' => RealRashid\SweetAlert\Facades\Alert::class,
],
php artisan vendor:publish --provider="RealRashid\SweetAlert\SweetAlertServiceProvider"
This will create a sweet-alert.php
file under the config
directory of your application.
sweet-alert
script and stylesheets in your Blade template:<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/sweetalert2@11.1.1/dist/sweetalert2.min.css">
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11.1.1/dist/sweetalert2.all.min.js"></script>
Alert
facade in your controllers or views to display SweetAlert dialogs:use Alert;
Alert::success('Title', 'Message');
Alert::error('Title', 'Message');
Alert::warning('Title', 'Message');
Alert::info('Title', 'Message');
Alert::question('Title', 'Message');
SweetAlert Laravel provides multiple colors for different types of alerts. You can use any of the following methods:
Alert::success('Title', 'Message');
Alert::error('Title', 'Message');
Alert::warning('Title', 'Message');
Alert::info('Title', 'Message');
Alert::question('Title', 'Message');
SweetAlert Laravel allows you to chain multiple alerts together using the queue
method:
Alert::success('Title', 'Message')
->warning('Title', 'Message')
->error('Title', 'Message');
SweetAlert Laravel provides several customization options for your dialogs. You can pass an array of options to the options
method:
Alert::success('Title', 'Message')
->options([
'confirmButtonText' => 'OK',
'cancelButtonText' => 'Cancel',
'reverseButtons' => true,
]);
SweetAlert Laravel provides a convenient way to display flash messages using the toast
method:
return redirect('/home')->toast('Welcome back, John!');
SweetAlert Laravel supports several types of callbacks for your dialogs. For example, you can use the confirmButtonColor
option to change the color of the confirm button and use the confirm
callback to execute some code when the user confirms the dialog:
Alert::warning('Are you sure?', 'This action cannot be undone')
->confirmButtonColor('#3085d6')
->confirm('Yes, delete it', function () {
// Code to execute when the user confirms the dialog
})
->cancel('No, cancel');
SweetAlert Laravel is a powerful and easy-to-use package that integrates the SweetAlert library into your Laravel applications. With its multiple colors, chaining alerts, customization options, flash messages, and callbacks, you can create beautiful and functional dialogs that enhance your user experience. Give it a try and see the difference!