I am not using resource controller.
The route:
Route::delete('/deleteTag/{tag}','Controller2#deleteTag');
The controller function:
public function deleteTag(Tag $tag){
$Tag = Tag::where('id', $tag->id)->get()->first();
$Tag->delete();
return redirect()->action('Controller2#main');
}
The call:
<form method="delete" action="http://***/public/deleteTag/{{$tag->id}}">
{!! Form::token() !!}
<button type="submit">delete</button>
</form>
The program returns a MethodNotAllowedHttpException.
Thank you.
You may try this (Notice the hidden _method input):
<form method="post" action="http://***/public/deleteTag/{{$tag->id}}">
{!! Form::token() !!}
<input type="hidden" name="_method" value="DELETE">
<button type="submit">delete</button>
</form>
Check Form Method Spoofing.
Update:
In the latest versions of Laravel, it's possible to use blade directives for csrf and method in the form, for example:
<form method="post" action="...">
#csrf
#method('DELETE')
<button type="submit">delete</button>
</form>
It's better to change your route to this mode:
Route::resource('tags','TagController');
You should register a resourceful route to the controller. This single route declaration creates multiple routes to handle a variety of RESTful actions on the Tag resource.
Remember, since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs.
<input type="hidden" name="_method" value="DELETE">
or add this in your form
{{method_field('DELETE')}}
Related
Hey guys i have an error when i submit the delete it says that i'm not sending the id to the route i think,
that's the code to send the id to the route (and sorry for my english),
<form id="del_type" action="{{ route('admin.event.destroy',$event->id)}}" method="post">
{!! method_field('delete') !!}
{{ csrf_field() }}
<button class="btn btn-danger" type="submit" id="del_id">Supprimer</button>
it's wierd cause when i submit it's deleting the element from the database but with this error and in the URI i can see the id
take a look at the routes (All route names are prefixed with 'admin.')
Route::delete('evenment/event/{id}','EventController#destroy')->name('event.destroy');
change route to
admin.event.destroy
Change action to this and try
{{ route('admin.event.destroy', ['id' => $event->id])}}
I am working on basic CRUD using Laravel. I am getting MethodNotAllowedHttpException when using PUT and DELETE method in Laravel form action. GET and POST action methods work fine.
HTML form only accept either GET or POST method so you can't use PUT and DELETE in form method. However, if you want to use PUT or DELETE then laravel provide Form method spoofing like this
<input type="hidden" name="_method" value="PUT">
Here is the form example
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Short form
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>
Route
Route::put('foo/bar', 'FooController#bar');
Check details here https://laravel.com/docs/5.6/routing#form-method-spoofing
I'm using resource controller in Laravel 5.3 and I'm having problem with deleting a record. I would like to use simple HTML code and I know that I have to add a hidden method input to make it work.
My code is very simple:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
<input type="submit" value="Delete" />
</form>
After clicking submit app redirects to blank page - it doesn't go to destroy function in controller. I don't have any idea, why it's not working. I'm not using facades, is it necessary in operation like this? I'll be very glad for every tip, thank you.
You're most likely running into a TokenMismatchException. Laravel considers the DELETE method a "writable" method, so it expects a CSRF token.
You can either add a CSRF token to your form, or, if appropriate, you can add your URI to the except array in your app/Http/Middleware/VerifyCsrfToken.php file.
To add the token to your form:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" />
</form>
To make my life simpler in a huge website, can I do this?
Route::get('view/{id}', 'PostController#show')->name('post');
Route::delete('view/{id}', 'PostController#delete')->name('post');
Route::post('view/{id}', 'PostController#save')->name('post');
And then in my form, I can do this.
<!-- Delete Form -->
<form method="post" action="{{ route('post', $post->id) }}">
<input type="hidden" name="_method" value="DELETE">
<button type="submit">Delete</button>
</form>
<!-- Edit Form -->
<form method="post" action="{{ route('post', $post->id) }}">
<input type="hidden" name="_method" value="PATCH">
<button type="submit">Edit</button>
</form>
<!-- Etc -->
Can I do this? Is this recommended?
Yes, in fact you can use the following to keep your route file clean: https://laravel.com/docs/5.2/controllers#restful-naming-resource-route-parameters
Route::resource('foobar', 'FooBarController');
This will auto generate RESTful routes for you:
if you run php artisan route:list you can see all the HTTP routes.
I have a PostController in which I have all RESTful methods. I can generate route to delete method by defining method in form tag like below,
<form action="{{route('post.destroy', [$post->id])}}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" value="Delete">
</form>
But I need to generate same route with link,
Delete
thanks.
You have to create an link, which triggers your Form Submission with JavaScript. You cant create an Link with "POST" instead of "GET" Method.
As Basic example without JQuery and proper separation of code, add this to your template.
Add an ID to you Form "myform"
<script type="text/javascript">
function submitMyform()
{
document.myform.submit();
}
</script>
And change your link to:
Delete
if you want to show only the link, use hidden form elements.
This should do it:
Delete