HTML DELETE method on one button

I am using Laravel with a controller Route::resource()

and am deleting anything it needs to run a function destroy()

. This method requires the DELETE HTTP method to be disabled. How do I do this on one button?

thank

+3


source to share


3 answers


You can create a shape around the delete button. This will not add anything visually to the page.

For example:

{{ Form::open(['url' => 'foo/bar', 'method' => 'delete', 'class' => 'deleteForm']) }}
  <input type="submit" class="deleteBtn" />
{{ Form::close() }}

      



Laravel's form helper automatically tricks the form and adds a hidden field for the method DELETE

.

Then you can style the button using the class .deleteBtn

. If the button needs to be placed on a row, you can even assign a display: inline;

class to the property .deleteForm

.

+3


source


You can add a form and use Laravel Ways to Create Forms

<input type="hidden" name="_method" value="DELETE">

      



or you can use ajax (example code below uses jQuery)

$.ajax({
    url: 'YOUR_URL',
    type: 'DELETE',
    success: function(result) {
        // Do something with the result
    }
});

      

+1


source


You can also use this library https://gist.github.com/soufianeEL/3f8483f0f3dc9e3ec5d9 to implement this:

<a href="posts/2" data-method="delete" data-token="{{csrf_token()}}" data-confirm="Are you sure?">

      

0


source







All Articles