In our third tutorial in this series, we have added in our admin panel a delete button to delete one message. The problem here is that when we click on that button, it directly deletes the message without asking us to confirm the deletion. In this tutorial, we’ll add a beautiful confirmation message box to that button before deleting that entry.
This blog post is a part of a series that introduces you to the Laravel world by building a simple contact form protected by Google ReCaptcha and an admin panel to manage messages sent through our contact form.
To follow us along this tutorial, download the source code we produced in the previous tutorial here. We advise you to read the previous tutorial if you are confused because the dependencies we are using here have been added in the previous tutorial. By dependencies, I talk about JQuery and Sweetalert2.
The list-messages.blade.php view file
Open the file resources/views/list-messages.blade.php and add the following script to the script section.
script.js
$(".delete-message").on("submit", function(e) {
e.preventDefault();
let button = $(this)
Swal.fire({
icon: "warning",
title: "Are you sure?",
text: "You won't be able to revert this.",
showCancelButton: true,
confirmButtonText: "Yes, delete it!"
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
type: "POST",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
},
url: button.data("route"),
data: {
"_method": "delete"
},
success: function(response, textStatus, xhr) {
Swal.fire({
icon: "success",
text: response,
showDenyButton: false,
showCancelButton: false,
confirmButtonText: "Go on",
}).then((result) => {
window.location = '/admin/view-messages'
})
}
})
}
})
Also, modify the delete button from the following code:
<form method="post" action="{{ url('/admin/messages/' . $message->id . '/delete') }}" style="display:inline">
{{ method_field('DELETE') }}
@csrf
<button type='submit' class="btn btn-danger btn-sm">Delete</button>
</form>
To the following one:
<form method="post" class="delete-message"
data-route="{{ url('/admin/messages/' . $message->id . '/delete') }}"
style="display:inline">
<button type='submit' class="btn btn-danger btn-sm">Delete</button>
</form>
To finish, modify the delete_message method from:
public function delete_message($id)
{
$message = Contact::find($id);
if ($message) {
$message->delete();
}
return redirect('/admin/view-messages');
}
To
public function delete_message($id)
{
$message = Contact::find($id);
if ($message) {
$message->delete();
}
return response('Message delete successfully', 200);
}
Testing
Start your development server with the command php artisan serveand try this feature in your admin panel

Confirmation message for deletion

Message after deletion
Conclusion
In this tutorial, we have explained how to add a beautiful confirmation delete message to our delete message buttons. If you encounter a problem at any step and need more explanation, just write to us. The complete code for this tutorial is found here.
If development isn’t your field, consider hiring Laravel specialists who can supercharge your application’s scalability and performance with precision.