cheese5505
cheese5505

Reputation: 982

HTML DELETE Method on a single button

I'm using Laravel with a Route::resource() controller and to delete something it needs to run the function destroy(). This method requires a DELETE HTTP method to go off. How do I do this on a single button?

Thanks

Upvotes: 6

Views: 14659

Answers (3)

Juan Girini
Juan Girini

Reputation: 1168

You could 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?">

Upvotes: 0

karanmhatre
karanmhatre

Reputation: 356

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

For example:

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

The Laravel Form helper automatically spoofs the form and adds the hidden field for the DELETE method.

Then you can style the button using the .deleteBtn class. If the button needs to be positioned inline, you can even assign a display: inline; property to the .deleteForm class.

Upvotes: 3

chanafdo
chanafdo

Reputation: 5124

You could add a form and use Laravel's Form Method Spoofing

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

or you could use ajax (Example code below uses jQuery)

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

Upvotes: 2

Related Questions