简体   繁体   中英

delete confirmation in laravel

I have the following code:

@foreach($results as $result)
  <tr>
    <td>{{$result->my_id}}</td>
    <td>{{$result->province_name}}</td>
    <td>{{$result->city_name}}</td>
    <td>
      <a class="btn btn-primary" href="{{route('city-edit', $result->my_id)}}"><i class="fa fa-edit"></i></a>
      <a class="btn btn-danger" href="{{route('city-delete', $result->my_id)}}"><i class="fa fa-trash"></i></a>
    </td>
  </tr>
@endforeach

How to add a confirmation on delete each data?

我更喜欢一种更简单的方法,只需添加onclick="return confirm('Are you sure?')" ,如下所示:

<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>

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.

  <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');

If this is your link:

<a href="{{route('venuepropertyarea.delete', ['propertyarea' => $propertyareaname->id])}}" data-method="DELETE" data-confirm="Are you sure to delete this item?" class="btn btn-danger btn-xs pull-right delete"><i class="fa fa-trash"></i> </a>

Route:

Route::get('/icrm/venues/property_area/delete/{propertyarea}', 'VenuePropertyAreaController@deletepropertyarea')->name('venuepropertyarea.delete');

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.

index.blade.php

<form action="{{route('todos.destroy', $todo->Id)}}" method="post">
              <input type="hidden" name="_method" value="DELETE">
              @csrf
      <button id="btnDelete"class="btn btn-danger btn-sm">Delete</button>
     </form>
<script type="javascript">
       document.onsubmit=function(){
           return confirm('Sure?');
       }
</script>

I used pure PHP Form, the method DELETE has to be passed as hidden on the submit page action, so I catch it trough javascript and I get the confirmation alert.

<a  class="btn btn-danger px-5  radius-30" onclick="return confirm('Are you sure you want to Unassign this agent?')" href="{{ route('unassign.agent', $report->id) }}" >Unassign</a>

the right route method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM