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.
It is going to that page but it is not deleting
Updated!
deleted record but not show alert
solve its my error
The js code contains unknown variable “event”, I think you mean the “e” variable
Yes! thanks for giving reply.
Thank you so much! I used this script in Codeigniter and it works very well!
Awesome
Its deleting without giving any confirmation first. I followed all the steps.
Check sweetalert cdn.
thanks for this it working perfectly
But are deletes in Laravel not supposed to be POST events and not GETs?
Hey, in post you need to try this https://codingdriver.com/delete-with-sweet-alert-in-laravel-post-method.html
Tried it once on laravel 5.8 and it works like charm. But I used variable e not event.
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.
awesomwe !! its work for me. great thanks sir.
great work have you done brother
where should I enter my java script code