Laravel编辑后不工作

Laravel编辑后不工作

问题描述:

有路线Laravel编辑后不工作

Route::get('posts', '[email protected]'); 
Route::get('posts/create', '[email protected]'); 
Route::get('posts/{id}', '[email protected]')->name('posts.show'); 
Route::get('get-random-post', '[email protected]'); 
Route::post('posts', '[email protected]'); 
Route::post('publish', '[email protected]'); 
Route::post('unpublish', '[email protected]'); 
Route::post('delete', '[email protected]'); 
Route::post('restore', '[email protected]'); 
Route::post('change-rating', '[email protected]'); 

Route::get('dashboard/posts/{id}/edit', '[email protected]'); 
Route::put('dashboard/posts/{id}', '[email protected]'); 

Route::get('dashboard', '[email protected]'); 
Route::get('dashboard/posts/{id}', '[email protected]')->name('dashboard.show'); 
Route::get('dashboard/published', '[email protected]'); 
Route::get('dashboard/deleted', '[email protected]'); 

方法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]); 
} 

,但是当我改变后,点击提交按钮,我得到一个错误

RouteCollection.php中的MethodNotAllowedHttpException第233行:

有什么不对?如何解决它?

UPD

从视图

{!! Form::model($post, ['method'=> 'PATCH', 'action' => ['[email protected]', $post->id], 'id' => 'edit-post']) !!} 

和结果开放的形式我得到

<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"> 

偏偏这个动作http://mytestsite/dashboard/posts?6 ???

+0

您没有名为** dashboard.show **的路由** – Sebastian

+0

我给路由添加了名称,但错误保持不变 – Heidel

+0

您使用PUT方法更新您的帖子,因此请确保您有'您的表单中的{{method_field('PUT')}}' – Sebastian

根据错误消息,最可能的原因是操作和路由不匹配。可能路由需要POST方法,但操作是GET。核实。

+0

这不仅仅是一个回答 – Sebastian

+0

@Sebastian也许你是对的。但没有确切的代码,我认为这是一个答案:) –

尝试在您的路线中使用patch而不是put进行更新。

只是一个小技巧,你可以通过声明模型的参数,这样的节约能源和一点时间:

public function update(Post $id, PostRequest $request) 

和摆脱这种

$post = Post::findOrFail($id); 

编辑

您可以在表单中使用网址,而不是行动:

'url'=> '/mytestsite/dashboard/posts/{{$post->id}}' 

尝试发送帖子的ID在隐藏的输入,不采用SMT这样的“行动” => [“PostsController @更新”,$岗位 - > id] 它有助于结果动作网址。

+0

为什么不呢?这种“行动”有什么问题? – Heidel