Laravel Sweet Alert Confirm Delete Tutorial

May 10, 2021 . Admin

Hello Friends,

Now let's see example of how to use sweet alert in laravel. This is a short guide on laravel if sweet alert. We will use how to use sweet alert in laravel. Here you will learn how to use sweet alert in laravel. We will use how to use if sweet alert in laravel. Let's get started with how to sweet alert use in laravel.

Here i will give you many example how you can check sweet alert in laravel.

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 `students` (`id`, `name`, `email`, `created_at`, `updated_at`) VALUES
(1, 'Abc, 'Abc@gmail.com', NULL, NULL),
(3, 'Xyx, 'Xyx@gmail.com', 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.

following path:/routes/web.php

Route::get('admins', 'AdminController@index');
Route::delete('admins/{id}','AdminController@destroy')->name('admins.destroy');
Step 3: Create AdminController

In third step, we will create new AdminController 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:

<?php

	namespace App\Http\Controllers;

	use Illuminate\Http\Request;
	use App\Admin;

	class AdminController extends Controller
	{
	    public function index()
	    {
	      $admins = Admin::get();
	      return view('admin',compact('admins'));
	    }

	    public function destroy(Request $request,$id)
	    {
	      Admin::where('id',$id)->delete();
	      return back();
	    }
	}
?>
Step 4: Create admin blade file

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

<!DOCTYPE html>
<html>
	<head>
	  <title>Laravel Sweet Alert Confirm Delete Tutorial-MyWebtuts.com</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/sweetalert/1.1.3/sweetalert.min.css" />
	  <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.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 Alert Confirm Delete Tutorial-MyWebtuts.com</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($admins as $admin)
	                <tr>
	                  <td>{{ $admin->name }}</td>  
	                  <td>{{ $admin->email }}</td>  
	                  <td>
	                    <button class="btn btn-danger btn-flat btn-sm remove-user" data-id="{{ $admin->id }}" data-action="{{ route('admins.destroy',$admin->id) }}"> Delete</button>
	                  </td>  
	                </tr>
	                @endforeach
	            </table>
	          </div>
	        </div>
	      </div>
	    </div>
	  </div>
	<script type="text/javascript">
	  $("body").on("click",".remove-user",function(){
	    var current_object = $(this);
	    swal({
	        title: "Are you sure?",
	        text: "You will not be able to recover this imaginary file!",
	        type: "error",
	        showCancelButton: true,
	        dangerMode: true,
	        cancelButtonClass: '#DD6B55',
	        confirmButtonColor: '#dc3545',
	        confirmButtonText: 'Delete!',
	    },function (result) {
	        if (result) {
	            var action = current_object.attr('data-action');
	            var token = jQuery('meta[name="csrf-token"]').attr('content');
	            var id = current_object.attr('data-id');

	            $('body').html("<form class='form-inline remove-form' method='post' action='"+action+"'></form>");
	            $('body').find('.remove-form').append('<input name="_method" type="hidden" value="delete">');
	            $('body').find('.remove-form').append('<input name="_token" type="hidden" value="'+token+'">');
	            $('body').find('.remove-form').append('<input name="id" type="hidden" value="'+id+'">');
	            $('body').find('.remove-form').submit();
	        }
	    });
	});
	</script>
	</body>
</html>

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:

http://localhost:8000/admins

It will help you....

#Laravel