📜  sweetalert laravel - PHP (1)

📅  最后修改于: 2023-12-03 15:20:24.615000             🧑  作者: Mango

SweetAlert Laravel - PHP

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.

Installation
Requirements
  • Laravel 5.5 or higher
  • PHP 7.1 or higher
  • SweetAlert 2
Steps
  1. Install the package via composer:
composer require realrashid/sweet-alert
  1. If you are using Laravel 5.5 or higher, the package will automatically register the service provider. But if you are using Laravel 5.4 or lower, you need to add the provider to your config/app.php file:
'providers' => [
    // other providers
    RealRashid\SweetAlert\SweetAlertServiceProvider::class,
],
  1. Add the following line to the aliases section of your config/app.php file:
'aliases' => [
    // other aliases
    'Alert' => RealRashid\SweetAlert\Facades\Alert::class,
],
  1. Publish the package's configuration file:
php artisan vendor:publish --provider="RealRashid\SweetAlert\SweetAlertServiceProvider"

This will create a sweet-alert.php file under the config directory of your application.

  1. Include the 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>
  1. You can now use the 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');
Features
Multiple Colors

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');
Chaining Alerts

SweetAlert Laravel allows you to chain multiple alerts together using the queue method:

Alert::success('Title', 'Message')
    ->warning('Title', 'Message')
    ->error('Title', 'Message');
Customization

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,
    ]);
Flash Messages

SweetAlert Laravel provides a convenient way to display flash messages using the toast method:

return redirect('/home')->toast('Welcome back, John!');
Callbacks

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');
Conclusion

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!