Reputation: 3262
There are routes
Route::get('posts', 'PostsController@index');
Route::get('posts/create', 'PostsController@create');
Route::get('posts/{id}', 'PostsController@show')->name('posts.show');
Route::get('get-random-post', 'PostsController@getRandomPost');
Route::post('posts', 'PostsController@store');
Route::post('publish', 'PostsController@publish');
Route::post('unpublish', 'PostsController@unpublish');
Route::post('delete', 'PostsController@delete');
Route::post('restore', 'PostsController@restore');
Route::post('change-rating', 'PostsController@changeRating');
Route::get('dashboard/posts/{id}/edit', 'PostsController@edit');
Route::put('dashboard/posts/{id}', 'PostsController@update');
Route::get('dashboard', 'DashboardController@index');
Route::get('dashboard/posts/{id}', 'DashboardController@show')->name('dashboard.show');
Route::get('dashboard/published', 'DashboardController@published');
Route::get('dashboard/deleted', 'DashboardController@deleted');
methods in PostsController
public function edit($id)
{
$post = Post::findOrFail($id);
return view('dashboard.edit', compact('post'));
}
public function update($id, PostRequest $request)
{
$post = Post::findOrFail($id);
$post->update($request->all());
return redirect()->route('dashboard.show', ["id" => $post->id]);
}
but when I change post and click submit button, I get an error
MethodNotAllowedHttpException in RouteCollection.php line 233:
What's wrong? How to fix it?
upd
opening of the form from the view
{!! Form::model($post, ['method'=> 'PATCH', 'action' => ['PostsController@update', $post->id], 'id' => 'edit-post']) !!}
and as result I get
<form method="POST" action="http://mytestsite/dashboard/posts?6" accept-charset="UTF-8" id="edit-post"><input name="_method" type="hidden" value="PATCH"><input name="_token" type="hidden" value="aiDh4YNQfLwB20KknKb0R9LpDFNmArhka0X3kIrb">
but why this action http://mytestsite/dashboard/posts?6
???
Upvotes: 1
Views: 1213
Reputation: 2179
Try to use patch
instead of put
in your route for updating.
Just a small tip you can save energy and a bit of time by declaring the Model in your parameters like this:
public function update(Post $id, PostRequest $request)
and get rid of this
$post = Post::findOrFail($id);
EDIT
You can use url in your form instead of action :
'url'=> '/mytestsite/dashboard/posts/{{$post->id}}'
Upvotes: 2
Reputation: 152
Try to send post id in hidden input, don't use smt like that 'action' => ['PostsController@update', $post->id] It contribute to result action url.
Upvotes: 0
Reputation: 261
Based on the error message, the most probable reason is the mismatch between action and route. Maybe route requires POST method, but the action is GET. Check it.
Upvotes: 0