Programmmereg
Programmmereg

Reputation: 819

How to delete one row using resource routes?

i want to delete one row from database but I am getting an error. Here's my code. How to delete that?

Route::resource('x', 'xController', ['only' => [
    'index', 'store', 'destroy'
]]);

<a href="{{ action('xController@destroy', $x->id) }}" class="btn btn-danger">Delete</a>

MethodNotAllowedHttpException in RouteCollection.php line 218:

Upvotes: 1

Views: 57

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

You need to use a form with DELETE method since you're using resource controller.

<form method="POST" action="{{ action('xController@destroy', $x->id) }}">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <input type="submit" value="Delete" class="btn btn-danger">
</form>

Upvotes: 0

Erik Berkun-Drevnig
Erik Berkun-Drevnig

Reputation: 2356

You need to use Form Method Spoofing,

<form action="/foo/bar" method="POST">
    {{ method_field('DELETE') }}
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method.

Upvotes: 1

Related Questions