Hello Devs,

In this tutorial, we are going to learn about laravel 7/6 sweetalert record delete. Given below is the short example of sweet alert2 for laravel 7/6.

Follow this step by step guide given below:




Step 1: 


Add Table and Dummy Records

INSERT INTO `products` (`id`, `name`, `email`, `created_at`, `updated_at`) VALUES
(1, 'Mln, 'Pqr@gmail.com', NULL, NULL),
(3, 'Demo, 'Demo@gmail.com', NULL, NULL),



Step 2: 


Add New Route routes/web.php

Route::get('users', 'TestController@index');
Route::post('users/{id}','TestController@destroy')->name('users.destroy');



Step 3: 


Create UserController

<?php

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)
    {
      User::where('id',$id)->delete();
      return back();
    }
}



Step 4:


 Create user blade file

<!DOCTYPE html>
<html>
<head>
  <title>Laravel Sweet Alert2 Confirm Delete Example-rathorji.in</title>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css">
  <script src="http://demo.itsolutionstuff.com/plugin/jquery.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.2.0/sweetalert2.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.2.0/sweetalert2.all.min.js"></script>
  <meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<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-rathorji.in</h5>
          </div>
          <div class="card-body">
            <table class="table table-bordered">
              <tr>
                <td>Name</td>
                <td>Email</td>
                <td width="5%">Action</td>
              </tr>
                @foreach($users as $user)
                <tr>
                  <td>{{ $user->name }}</td>  
                  <td>{{ $user->email }}</td>  
                  <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>
                  </td>  
                </tr>
                @endforeach
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
<script type="text/javascript">
    function deleteConfirmation(id) {
        swal({
            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');

                $.ajax({
                    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 {
                e.dismiss;
            }

        }, function (dismiss) {
            return false;
        })
    }
</script>
</body>
</html>


Run this command:

php artisan serve


Open this URL:

http://localhost:8000/users


I hope this example helps you.