Delete Confirmation Alert Dialog using Sweetalert in Laravel

Deleting data with sweetalert confirmation is common in laravel application. Here we will use sweet alert confirmation box with the cancel and yes button in php laravel before deleting the row in laravel. Below we use sweetalert cdn to showing confirm box alert before deleting any row from laravel blade file.

The sweet alert in any project is very important because the confirmation delete is very required to making confirm once to the user is satisfied to delete your record. So Laravel Delete with Sweetalert uses all project.

Let’s Delete method with Sweet Alert in Laravel 8, laravel 9 or Laravel 10 versions as well easy way step by step from scratch.

Step 1: Add Route

The first step is to add a route for deleting data from the database.

use App\Controllers\PostController;

Route::get('posts/delete/{id}', 'PostController@destroy');

Step 2: Add Delete Method

Add method in your controller.

public function destroy($id)
{
    Post::find($id)->delete();
    return back()->with('success','Post deleted successfully');
}

Step 3: In Blade File

In your blade file add the class “delete-confirm” in your posts listing blade file.

<a href="/post/delete/{{$post->id}}" class="button delete-confirm">Delete</a>

Step 4: In js File

In the last step add the below code in your JavaScript file.

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

$('.delete-confirm').on('click', function (event) {
    event.preventDefault();
    const url = $(this).attr('href');
    swal({
        title: 'Are you sure?',
        text: 'This record and it`s details will be permanantly deleted!',
        icon: 'warning',
        buttons: ["Cancel", "Yes!"],
    }).then(function(value) {
        if (value) {
            window.location.href = url;
        }
    });
});

I hope this work for you. If any questions laravel delete data with sweet-alert confirmations alert box then comment, please.

19 thoughts on “Delete Confirmation Alert Dialog using Sweetalert in Laravel”

  1. sir i hove some question,
    how to make your code :

    $(‘.delete-confirm’).on(‘click’, function (event) {
    event.preventDefault();
    const url = $(this).attr(‘href’);
    swal({
    title: ‘Are you sure?’,
    text: ‘This record and it`s details will be permanantly deleted!’,
    icon: ‘warning’,
    buttons: [“Cancel”, “Yes!”],
    }).then(function(value) {
    if (value) {
    window.location.href = url;
    }
    });
    });

    into another function, for example, if I enter a page then there is an if statement, and that sweetalert pops up and tells the if statement.

    Reply

Leave a Comment