2017-03-07 245 views
1

有路線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

回答

0

根據錯誤消息,最可能的原因是操作和路由不匹配。可能路由需要POST方法,但操作是GET。覈實。

+0

這不僅僅是一個回答 – Sebastian

+0

@Sebastian也許你是對的。但沒有確切的代碼,我認爲這是一個答案:) –

2

嘗試在您的路線中使用patch而不是put進行更新。

只是一個小技巧,你可以通過聲明模型的參數,這樣的節約能源和一點時間:

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

和擺脫這種

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

編輯

您可以在表單中使用網址,而不是行動:

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

嘗試發送帖子的ID在隱藏的輸入,不採用SMT這樣的「行動」 => [「PostsController @更新」,$崗位 - > id] 它有助於結果動作網址。

+0

爲什麼不呢?這種「行動」有什麼問題? – Heidel