Laravel 5 MethodNotAllowedHttpException PUT

2019-07-10 16:19发布

问题:

I am trying to update a user, but when I hit the submit button, Laravel throws the below error:

"RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 206".

I think that the PUT method is not allowed, but I do not understand the reason. The request never reaches UserController@update.

I have configured a resource route like this:

Route::resource('backend/users', 'Backend\UsersController');

The output of php artisan route:list is:

回答1:

I solved the problem like this: it must be the form's post action error;

<form method="POST" action="10.241.229.1/backend/users/{{$user->id}}"; accept-charset="UTF-8">

add the id you want update to the action.



回答2:

use put method like this within form,for more https://laravel.com/docs/5.2/routing#form-method-spoofing

{{ method_field('PUT') }}


回答3:

Coming a bit late on this question.

In my experience this kind of error comes for two reasons:

  1. as Laravel docs say, 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.

  2. if you are making the request from a HTML form, and you have the VerifyCsrfToken middleware enable, than you will need to add a hidden _token field to the form with {{ csrf_token() }} as value.