Laravel delete record with sweetalert using post method example tutorial show you how to Delete record with sweet alert 2 using post method.
In laravel resources controller deleting data with post method using sweet alert is something different according to deleting data with get method because in get method we send id from blade file to controller file.
Step 1: Make Route
Route::resource('tasks', 'TaskController');
Step 2: Post Method Link Blade File
<form action="{{ route('tasks', [$task->id]) }}" method="post">
@csrf
<button class="btn btn-danger btn-sm delete-confirm" data-name="{{ $task->name }}" type="submit">Delete</button>
</form>
Step 3: In js File
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.0/sweetalert.min.js"></script>
$('.delete-confirm').click(function(event) {
var form = $(this).closest("form");
var name = $(this).data("name");
event.preventDefault();
swal({
title: `Are you sure you want to delete ${name}?`,
text: "If you delete this, it will be gone forever.",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
form.submit();
}
});
});
Step 4: Add Delete Method
public function destroy(Task $task)
{
$task->delete();
return back();
}
In laravel get method delete method using sweetalert is very easy and the post method also has easy steps you need to follow above steps. I hope this work for you. If you have any questions please comment below.
thanks
Works. Thank you.
doesn’t work, doesn’t even show the pop up box
I think you have not include the model in your base file.