I want to update a post. Creating and deleting a post works perfectly but whenever I try to update it with a PATCH form, it fails and gives a MethodNotAllowedHttpException
.
My routes.php:
...
Route::resource('posts', 'PostsController');
...
Which gives me the following list of possible routes (pasted in css to keep it readable):
| GET|HEAD | posts | posts.index | App\Http\Controllers\PostsController@index
| POST | posts | posts.store | App\Http\Controllers\PostsController@store
| GET|HEAD | posts/create | posts.create | App\Http\Controllers\PostsController@create
| GET|HEAD | posts/{posts} | posts.show | App\Http\Controllers\PostsController@show
| DELETE | posts/{posts} | posts.destroy | App\Http\Controllers\PostsController@destroy
| PUT|PATCH | posts/{posts} | posts.update | App\Http\Controllers\PostsController@update
| GET|HEAD | posts/{posts}/edit | posts.edit | App\Http\Controllers\PostsController@edit
My edit.blade.php (url= localhost:8000/posts/1/edit):
{!! Form::model($post, ['method' => 'PATCH', 'action' => ['PostsController@update', $post]]) !!}
@include('posts/_form', array('submitText' => 'Update'))
{!! Form::close() !!}
And my PostsController:
public function update(Request $request, Post $post) {
$post->update($request->all());
return Redirect::route('posts.index')->with('message_succes', 'Post updated');
}
Whatever I try, it fails with a
MethodNotAllowedHttpException RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 206
Looking at the html source of the form the PATCH and token is inserted properly. When changing the PATCH to post in the form, it will use the store function and creates a new post. What do I need to do to update the post?
$post
. As you you didn't shareedit()
method, we don't know what returns in$post
variable. If $post is a collection, use['PostController@update',$post->id]
. If not work, please share theedit()
method's code. – smartrahat