Laravel 5 MethodNotAllowedHttpException PUT

£可爱£侵袭症+ 提交于 2019-12-11 02:13:42

问题


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.



来源:https://stackoverflow.com/questions/35431617/laravel-5-methodnotallowedhttpexception-put

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!