8

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

I have set up RESTful Resource Controllers:

Routes.php:

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

This should give me access to: (as displayed 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 the MethodNotAllowedHttpException error:

/users/testuser/playlists/1/edit

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

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

tereško
  • 58,060
  • 25
  • 98
  • 150
maarten
  • 123
  • 1
  • 1
  • 6

4 Answers4

17

In Laravel 5 and up:

<form method="POST" action="patchlink">
    @method('patch')
    . . .
</form>
bunbun
  • 2,595
  • 3
  • 34
  • 52
Sean
  • 460
  • 5
  • 9
14

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

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

Official documentation for helper function method_field()

Wistar
  • 3,770
  • 4
  • 45
  • 70
Alex Lomia
  • 6,705
  • 12
  • 53
  • 87
9

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

<input type="hidden" name="_method" value="PATCH">
Nenad
  • 3,438
  • 3
  • 28
  • 36
  • 1
    PATCH is being added via the method option in Form::open. You could also use PUT. I am having the same inexplicable issue, no matter what I do, the PUT or PATCH methods throw the MethodNotAllowedHttpException. Very frustrating. – Michael A Nov 05 '14 at 00:41
  • Try with `Route::any('the_url_where_you_post_to', function(){});` or accordingly to the method you use `Route::patch` or `Route::put` – Nenad Nov 05 '14 at 13:02
5

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.

MCH
  • 986
  • 13
  • 19