Laravel Sweet Alert2 Confirm Delete Example

Admin   Laravel   817  2021-06-13 17:56:02

Hi Guys

Now, let's see post of laravel 7/6 sweetalert record delete. you'll learn laravel sweet alert2 box using data delete. if you have question about laravel sweetalert2 then i will give simple example with solution. This post will give you simple example of sweet alert2 for laravel 7/6 example. you will do the following things for laravel 7/6 sweetalert2 using record delete.

In this example,we will learn how to open sweetalert2 before deleting user in laravel 7/6 application. I will show sweetalert2 jquery plugin using delete in laravel 7/6.I will show easy example of sweet alert2 before deleting user in laravel 7/6.

Sweetalert2.js provide us very simple it's simply use event of their plugin. So let's see bellow full example and you can implement sweet alert2 model before delete record in your web application.

Here the example of sweetalert2 before deleting user in laravel 7/6.

Step 1: Add Table and Dummy Records

I always prefer to give example from scratch, So in this example we will create "users" table with id, name, email, created_at and updated_at column. So you have to create table using migration and run that. After created successfully create table, make sure add some dummy data like as bellow


INSERT INTO `products` (`id`, `name`, `email`, `created_at`, `updated_at`) VALUES

(1, 'Mln, '[email protected]', NULL, NULL),

(3, 'Demo, '[email protected]', NULL, NULL),

Step 2: Add New Route

In this step, we are doing from scratch so we will add two routes, one for display data and another for delete request. So you have to simply add two new routes in your laravel application.



Route::get('users', '[email protected]');

Route::post('users/{id}','[email protected]')->name('users.destroy');

Step 3: Create UserController

In third step, we will create new UserController file to handle request of created two new route. In this Controller we define two method, index() and destroy(). Both method will handle route request. So let's create new controller and put code:



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class TestController extends Controller


public function index()


$users = User::get();

return view('user',compact('users'));


public function destroy(Request $request,$id)



return back();



Step 4: Create user blade file

In last step we will create user.blade.php file and write code of display user detail and sweet alert code.


<!DOCTYPE html>



<title>Laravel Sweet Alert2 Confirm Delete Example-</title>

<link rel="stylesheet" type="text/css" href="">

<script src=""></script>

<link rel="stylesheet" href="">

<script src=""></script>

<meta name="csrf-token" content="{{ csrf_token() }}">


<body class="bg-dark">

<div class="container">

<div class="row">

<div class="col-md-8 offset-md-2">

<div class="card mt-5">

<div class="card-header">

<h5>Laravel Sweet Alert2 Confirm Delete Example-</h5>


<div class="card-body">

<table class="table table-bordered">




<td width="5%">Action</td>


@foreach($users as $user)


<td>{{ $user->name }}</td>

<td>{{ $user->email }}</td>


<button class="btn btn-danger btn-flat btn-sm remove-user" data-id="{{ $user->id }}" data-action="{{ route('users.destroy',$user->id) }}" onclick="deleteConfirmation({{$user->id}})"> Delete</button>










<script type="text/javascript">

function deleteConfirmation(id) {


title: "Delete?",

text: "Please ensure and then confirm!",

type: "warning",

showCancelButton: !0,

confirmButtonText: "Yes, delete it!",

cancelButtonText: "No, cancel!",

reverseButtons: !0

}).then(function (e) {

if (e.value === true) {

var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');


type: 'POST',

url: "{{url('/users')}}/" + id,

data: {_token: CSRF_TOKEN},

dataType: 'JSON',

success: function (results) {

if (results.success === true) {

swal("Done!", results.message, "success");

} else {

swal("Error!", results.message, "error");




} else {



}, function (dismiss) {

return false;






Now we are ready to run our example so run bellow command for quick run:


php artisan serve

Now you can open bellow URL on your browser:



It will help you...