Laravel form will not be PATCH, only POST nested RESTfull controllers, MethodNotAllowedHttpException

I am trying to allow users to edit their playlist . However, whenever I try to execute a PATCH request, I get a MethodNotAllowedHttpException error message . (he is expecting a POST)

I installed RESTful Resource Controllers:

routes.php:

Route::resource('users', 'UsersController'); Route::resource('users.playlists', 'PlaylistsController'); 

This should give me access to: (as shown through php-artisan routes)

 URI | Name | Action PATCH users/{users}/playlists/{playlists} | users.playlists.update | PlaylistsController@update 

However, when I try to execute the following form, I get a MethodNotAllowedHttpException error:

/ users / testuser / playlists / 1 / change

 {{ Form::open(['route' => ['users.playlists.update', $playlist->id], 'method' => 'PATCH' ]) }} {{ Form::text('title', $playlist->title) }} {{ Form::close() }} 

If I remove 'method'=> 'PATCH' , I will not get an error, but it will execute my public function store() , and not my public function update()

+6
source share
4 answers

Write {!! method_field('patch') !!} {!! method_field('patch') !!} {!! method_field('patch') !!} {!! method_field('patch') !!} after the form:

 <form method="POST" action="patchlink"> {!! method_field('patch') !!} . . . </form> 

The official documentation for the helper function method_field()

+12
source

Since html forms only support GET and POST , you need to add an extra hidden field to the form called _method to simulate a PATCH request

 <input type="hidden" name="_method" value="PATCH"> 
+7
source

As suggested by @Michael A in the comment above, send it as a POST

 <form method="POST" action="patchlink"> <input type="hidden" name="_method" value="PATCH"> 

Worked for me.

+4
source

In Laravel 5 and above:

 <form method="POST" action="patchlink"> @method('patch') . . . </form> 
0
source

Source: https://habr.com/ru/post/1202625/


All Articles