How to Install & use Sweetalert in laravel |
How to Install & use Sweetalert in laravel |

How to Install & use Sweetalert in laravel


SweetAlert is a popular JavaScript library that allows developers to create beautiful and customizable alert messages. Integrating Install & use Sweetalert in laravel enhances the user experience by providing visually appealing and user-friendly pop-up alerts. In this tutorial, we will guide you through the process of installing SweetAlert in a Laravel project and demonstrate how to use it with practical code examples.

Step 1: Create a Laravel Project

If you don’t have a Laravel project set up, you can create one using the following command:

composer create-project --prefer-dist laravel/laravel sweetalert-demo

Change into the project directory:

cd sweetalert-demo

Step 2: Install SweetAlert

To install SweetAlert in your Laravel project, you can use npm. Run the following commands:

npm install sweetalert2

This command will install SweetAlert2, which is the latest version of SweetAlert.

Step 3: Include SweetAlert in Your Laravel Project

After installation, you need to include SweetAlert in your project. Open your resources/js/app.js file and add the following line:

import Swal from 'sweetalert2';
window.Swal = Swal;

Save the file and compile the assets using:

npm run dev

Step 4: Create a Sample Laravel Route

Open the routes/web.php file and add a sample route to test SweetAlert:

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SweetAlertController;

Route::get('/test-sweetalert', [SweetAlertController::class, 'testSweetAlert']);

Step 5: Create a Controller for SweetAlert

Generate a controller by running the following command:

php artisan make:controller SweetAlertController

Open the generated controller (app/Http/Controllers/SweetAlertController.php) and add the following code:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Alert;

class SweetAlertController extends Controller
    public function testSweetAlert()
        Alert::success('Success Title', 'Success Message');
        return view('welcome');

Step 6: Create a Blade View

Open the resources/views/welcome.blade.php file and add the following code:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SweetAlert Demo</title>
    <link href="{{ mix('css/app.css') }}" rel="stylesheet">
    <h1>SweetAlert Demo</h1>

    <script src="{{ mix('js/app.js') }}"></script>

Step 7: Run Your Laravel Application

Start your Laravel development server:

php artisan serve

Visit http://localhost:8000/test-sweetalert in your browser, and you should see a SweetAlert pop-up with a success message.


Integrating SweetAlert into your Laravel project can significantly enhance the user interface and provide a more engaging experience for users. With just a few simple steps, you can install SweetAlert and start using it to create stylish and responsive alert messages in your Laravel applications. Feel free to explore SweetAlert’s documentation for additional customization options and features.

Leave a Reply