Thursday, January 19, 2017

[Laravel 5][Resolved] patch update MethodNotAllowedHttpException

Got an error message :
(1/1)MethodNotAllowedHttpException

in RouteCollection.php (line 251)

at RouteCollection->methodNotAllowed(array('PATCH', 'DELETE'))
in RouteCollection.php (line 238)

at RouteCollection->getRouteForMethods(object(Request),array('PATCH', 'DELETE'))
in RouteCollection.php (line 176)

at RouteCollection->match(object(Request))
in Router.php (line 548)

at Router->findRoute(object(Request))
in Router.php (line 527)

at Router->dispatchToRoute(object(Request))
in Router.php (line 513)

at Router->dispatch(object(Request))
in Kernel.php (line 176)

at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))

It cause by sending a post request to a path but the target page register as HTTP verb PATCH in routes file.

html :

<form method="POST" action="http://localhost:1234/test/item/2" accept-charset="UTF-8" class="form-horizontal">

route file (\test\routes\web.php)

Route::patch ('item/{id}','TestController@update')->name('test.update');


Solution

Method 1 :

Add this line to your form, input type "hidden" with input name "_method" and value "PATCH":

<input name="_method" type="hidden" value="PATCH">
Method 2 :
If you use model binding , define your method as PATCH
Form::model($test ,["route"=>url("item/".$id) ,"method"=>"PATCH"])
So that form elements it would auto add a "<input name="_method" type="hidden" value="PATCH">" sentence after your opening tag of your form element.

Reference

https://stackoverflow.com/questions/25857650/laravel-form-wont-patch-only-post-nested-restfull-controllers-methodnotallo

No comments :

Post a Comment