Reputation: 83
I am using laravel 5.2 and I am unable to delete article in laravel. Below is my view link:
<form method="DELETE" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Below is my controller code:
public function destroy($id)
{
Article::destroy($id);
Session::flash('msg','Article deleted successfully');
return redirect()->back();
}
Upvotes: 0
Views: 62
Reputation: 2192
In your view file what you need to do is...
<form method="POST" action="/article/{{ $article->id }}">
<input type="hidden" name="_method" value="DELETE">
{{ csrf_field() }}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Upvotes: 0
Reputation: 16359
HTML forms don't actually support any methods other than GET
and POST
. To get around this Laravel spoofs the method and then picks this up in the request.
From the docs:
HTML forms do not support
PUT
,PATCH
orDELETE
actions. So, when definingPUT
,PATCH
orDELETE
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
As such, you just need to alter your form like so:
<form method="POST" action="/article/{{ $article->id }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
You can also generate the _method
with {{ method_field('DELETE') }}
using Blade.
Upvotes: 1