DEV Community


Posted on • Updated on

How to implement Delete Confirmation in Laravel 8, 7,6 with Modal

Hello devs, How are you doing? I am going to be showing you a little trick on how to implement delete confirmation using bootstrap modal.
In designing an application, we need to take into consideration that the user might not be that careful in using our application or unconsciously be pressing something on their phone or with the mouse and can accidentally click the delete button, so we need to make sure that in terms of delicate activities like deleting a data, there should be some level of confirmation, there are many ways to achieve this, you can use alert or confirm in JavaScript with jQuery, but we are going to be using bootstrap, this way it will look better and give us more chances of customizing how the confirmation will look.
Let's get into the business, We are going to be using the code from my previous article Laravel 8 CRUD App, A simple guide, the repo

Click on my profile to follow me to get more updates.

Step 1: Setup the app

  1. git clone
  2. cd laravel_8_crud/
  3. composer install
  4. npm install
  5. cp .env.example .env
  6. php artisan key:generate
  7. Add your database config in the .env file (you can check my articles on how to achieve that)
  8. php artisan migrate
  9. php artisan serve (if the server opens up,, then we are good to go) localhost
  10. Navigate to

Step 2: add delete route

Laravel resources route provides us with seven (7) methods that handle the CRUD operation, which means we have the delete method called destroy method already created. But we are going to add a new route called delete, what this route will do is redirect us to the modal that will execute the destroy method. So add the code below before the resource route for project

Route::get('projects/delete/{id}', [ProjectController::class, 'delete'])->name('delete');
Enter fullscreen mode Exit fullscreen mode

web route

Step 3: Create delete method

In our route, we indicate that we have a delete method in the project controller, so we need to create that, go to app/Http/Controllers/ProjectController.php and add the following method

public function delete($id)
    $project = Project::find($id);

    return view('projects.delete', compact('project'));
Enter fullscreen mode Exit fullscreen mode

The delete function accepts an id, which is the id of the item we want to delete, and return the delete view in our projects directory along with the id of the item.

Step 4: Create Delete view

In our delete method, we return the delete view, so we need to create that, go to resources/views/projects/ and create a delete blade file delete.blade.php and copy the code below and paste.

{{-- !-- Delete Warning Modal -->  --}}
<form action="{{ route('projects.destroy', $project->id) }}" method="post">
    <div class="modal-body">
        <h5 class="text-center">Are you sure you want to delete {{ $project->name }} ?</h5>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        <button type="submit" class="btn btn-danger">Yes, Delete Project</button>
Enter fullscreen mode Exit fullscreen mode

From the code above, we need to pass an id to the destroy method, the id of the item to be deleted, we also customize the delete message, so that the user will be sure what to delete.
The form will be the modal that will display for confirmation before deleting, the submit button will be the button that will execute the deleting, while the button with type button will cancel the form which will not delete the item.

Step 5: Edit the index.blade.php



<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Laravel 8 CRUD </h2>

<div class="pull-left ">
    <a class="btn btn-success" href="{{ route('projects.create') }}" title="Create a project"> <i class="fas fa-plus-circle"></i>
<div class="d-flex">
    <div class="mx-auto">

        <form action="{{ route('projects.index') }}" method="GET" role="search">

            <div class="d-flex">

                <button class="btn btn-info t" type="submit" title="Search projects">
                    <span class="fas fa-search"></span>
                <input type="text" class="form-control mr-2" name="term" placeholder="Search projects" id="term">
                <a href="{{ route('projects.index') }}" class="">
                    <button class="btn btn-danger" type="button" title="Refresh page">
                        <span class="fas fa-sync-alt"></span>


@if ($message = Session::get('success'))
<div class="alert alert-success">
    <p>{{ $message }}</p>

<table class="table table-bordered table-responsive table-hover">
    <thead class="thead-dark">
            <th scope="col">No</th>
            <th scope="col">Name</th>
            <th scope="col">Introduction</th>
            <th scope="col">Location</th>
            <th scope="col">Cost</th>
            <th scope="col">Date Created</th>
            <th scope="col">Action</th>
        @foreach ($projects as $project)
            <td scope="row">{{ ++$i }}</td>
            <td>{{ $project->name }}</td>
            <td>{{ $project->introduction }}</td>
            <td>{{ $project->location }}</td>
            <td>{{ $project->cost }}</td>
            <td>{{ date_format($project->created_at, 'jS M Y') }}</td>

                <a href="{{ route('', $project->id) }}" title="show">
                    <i class="fas fa-eye text-success  fa-lg"></i>

                <a href="{{ route('projects.edit', $project->id) }}">
                    <i class="fas fa-edit  fa-lg"></i>

                <a data-toggle="modal" id="smallButton" data-target="#smallModal" data-attr="{{ route('delete', $project->id) }}" title="Delete Project">
                    <i class="fas fa-trash text-danger  fa-lg"></i>


{!! $projects->links() !!}

<!-- small modal -->
<div class="modal fade" id="smallModal" tabindex="-1" role="dialog" aria-labelledby="smallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
            <div class="modal-body" id="smallBody">
                    <!-- the result to be displayed apply here -->

    // display a modal (small modal)
    $(document).on('click', '#smallButton', function(event) {
        let href = $(this).attr('data-attr');
            url: href
            , beforeSend: function() {
            // return the result
            success: function(result) {
            , complete: function() {
            , error: function(jqXHR, testStatus, error) {
                alert("Page " + href + " cannot open. Error:" + error);
            , timeout: 8000


Enter fullscreen mode Exit fullscreen mode

For the explanation of how I implement the modal functionality above, visit my previous article How to create modal in Laravel 8 and Laravel 6/7 with AJax

Edit the base layout of our app to add the jquery, ajax and bootstrap needed, go to resources/views/layouts/app.blade.php, copy and paste the code below.


    <title>App Name - @yield('title')</title>

    <link href=""

    <!-- Font Awesome JS -->
    <script defer src=""
        integrity="sha384-tzzSw1/Vo+0N5UhStP3bvwWPq+uvzCMfrN1fEFe+xBmv1C/AtVX5K0uZtmcHitFZ" crossorigin="anonymous">
    <script defer src=""
        integrity="sha384-6OIrr52G08NpOFSZdxxz1xdNSndlD4vdcf/q2myIUVO0VsqaGHJsB0RaBE01VTOY" crossorigin="anonymous">

    <link href="//" rel="stylesheet" id="bootstrap-css">

    <link href='' rel='stylesheet' type='text/css'>

  <script src=""></script>
    <script src='' type='text/javascript'></script>

        .footer {
            position: fixed;
            left: 0;
            bottom: 0;
            width: 100%;
            background-color: #9C27B0;
            color: white;
            text-align: center;

        body {
            background-color: #EDF7EF





    <div class="container">
    <div class="text-center footer">

        <h4>The writer needs a job</h4>
        <h4>+234 806 605 6233</h4>



Enter fullscreen mode Exit fullscreen mode

Restarting our server in case, it is not running and going to projects

php artisan serve , create some projects to test
projects index
Click on the trash icon to delete a project
delete modal

You can get the complete code from the Github repo.
Follow me for more of my articles, you can leave comments, suggestions, and reactions.
I am open to any vacancy as a PHP backend engineer, my strength is in the Laravel framework

click the link to view my profile and follow me

Visit my other posts

Top comments (0)