Laravel Sweet Alert Tutorial With Example

Laravel Sweet Alert Tutorial With Example is today’s leading topic. In this example, we will learn how to use sweet alert in laravel 5.6. Sweetalert is a Plugin which will make popup messages easy and pretty. We use uxweb/sweet-alert package. You can also use the sweet alert for confirmation before save or delete. We can also implement sweet alert instead of bootstrap confirmation. There are the different type of alert like a message, basic, info,success, error, warning.

Laravel Sweet Alert Tutorial With Example

We are going to Configure Laravel Project.

#1: Install Laravel Project

Install Laravel 5.6 Project by the typing following command.

composer create-project --prefer-dist laravel/laravel sweetalertexample

#2: Install sweet alert package

Next, we will install the sweet alert package through composer.

composer require uxweb/sweet-alert

As i install Laravel 5.6 so I don’t need to add Anything in providers and alias array within config/app.php

If you are using Laravel 5.4 or less version then you, include the service provider within config/app.php

'providers' => [
               	   UxWeb\SweetAlert\SweetAlertServiceProvider::class,
               ];

And, add a facade alias to this same file at the bottom.

‘aliases’ => [
               ‘Alert’ => UxWeb\SweetAlert\SweetAlert::class,
             ];

#3: Create one controller

php artisan make:controller SweetalertController

It will build the controller file called SweetalertController.php.

//SweetalertController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Alert;

class SweetalertController extends Controller
{
    public function notification($type)
    {
        switch ($type) {
            case 'message':
                Alert::message('Hello Investmentnovel','Message');
                break;
            case 'basic':
                Alert::basic('Welcome Investmentnovel','Basic');
                break;
            case 'info':
                Alert::info('Mail Sent','Info');
                break;
            case 'success':
                Alert::success('Your message has been successfully sent', 'Success');
                break;
            case 'error':
                Alert::error('Something went wrong','Error');
                break;
            case 'warning':
                Alert::warning('Please activate your account','Warning');
                break;
            default:
                break;
        }
        return view('sweetalert');
    }
}

#4: Define Route

We register all route in a web.php file.

//web.php

Route::get('sweetalert/{type}','SweetalertController@notification');

#5: Create a View File

You can create a file in resources/views/sweetalert.blade.php and put this following code in it.

<!-- sweetalert.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel Sweet Alert Tutorial With Example </title>
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" />
	<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.0/sweetalert.min.js"></script>
  </head>
  <body>
    <h2>Laravel Sweet Alert Tutorial With Example</h2>
    @include('sweet::alert')
  </body>
</html>

Run the project by typing following command.

php artisan serve

Now we can see the output in the below screenshot.

  1. Message:

Laravel Sweet Alert Example

2. Basic

Laravel Sweet Alert Tutorial

3. Info

Sweet Alert In Laravel Example

4. Success

How to implement sweet alert in laravel

5. Error

How to use sweet alert in laravel example

6. Warning

Laravel Sweet Alert Tutorial With Example

At last, our Laravel Sweet Alert Tutorial With Example is over.

Post Your Thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.