delete confirmation in laravel

If this is your link:

<a href="#" class="delete" data-confirm="Are you sure to delete this item?">Delete</a>

Use this Javascript:

var deleteLinks = document.querySelectorAll('.delete');

for (var i = 0; i < deleteLinks.length; i++) {
    deleteLinks[i].addEventListener('click', function(event) {
        event.preventDefault();

        var choice = confirm(this.getAttribute('data-confirm'));

        if (choice) {
            window.location.href = this.getAttribute('href');
        }
    });
}

Note: the <a> needs the delete in class. This solution uses Unobtrusive JavaScript and should work with IE 9 or newer.


I prefer a more easier way, just add onclick="return confirm('Are you sure?')", as bellow:

<a class="btn btn-danger" onclick="return confirm('Are you sure?')" href="{{route('city-delete', $result->my_id)}}"><i class="fa fa-trash"></i></a>

  <a class="btn btn-danger" onclick="return myFunction();" href="{{route('city-delete', $result->my_id)}}"><i class="fa fa-trash"></i></a>

<script>
  function myFunction() {
      if(!confirm("Are You Sure to delete this"))
      event.preventDefault();
  }
 </script>

<a href="{{ route('city-delete', $result->my_id) }}" 
   class="btn btn-danger" data-method="DELETE" data-confirm="Are you sure?"> Delete</a>

With brexis/laravel-data-method package, you can specify appropriate HTTP method and a confirmation text.

This is helpful if you have this for example into your routes file:

Route::get('cities/{city}', 'CityController@show')->name('city-show');
Route::delete('cities/{city}', 'CityController@destroy')->name('city-delete');