[英]Laravel route throws NotFoundHttpException
我正在尋找幫助。 我搜索了其他主題,大致了解了問題所在,但未成功將其修復到我的代碼中。 現在的問題是:當我嘗試提交代碼更新時,我遇到了NotFoundHttpException。
這是控制器和我的功能更新
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\T_collaborateurs_table;
class testing extends Controller
{
public function index()
{
$user = T_collaborateurs_table::all();
return view ("read", compact("user"));
}
public function create()
{
return view("create");
}
public function store(Request $Request)
{
T_collaborateurs_table::create(Request::all());
return redirect("index");
}
public function show($id)
{
$user=T_collaborateurs_table::find($id);
return view("show", compact("user"));
}
public function edit($id)
{
$user=T_collaborateurs_table::find($id);
return view("update", compact("user"));
}
public function update(Request $Request, $id)
{
$user = T_collaborateurs_table::find($id);
$user->update(Request::all());
return redirect("index");
}
}
現在的路線
Route::get("create", "testing@create");
Route::post("store", "testing@store");
Route::get("index", "testing@index");
Route::get("show/{id}", "testing@show");
Route::get("edit/{id}", "testing@edit");
Route::patch("update/{id}", "testing@update");
現在查看update.blade.php
<body>
{{Form::model($user, ['method'=>'patch', 'action'=>['testing@update',$user->id]])}}
{{Form::label('Id_TCa', 'ID')}}
{{Form::text('Id_TCa')}}
{{Form::label('Collaborateur_TCa', 'collab')}}
{{Form::text('Collaborateur_TCa')}}
{{Form::label('Responsable_TCa', 'resp')}}
{{Form::text('Responsable_TCa')}}
{{Form::submit("update")}}
{{Form::close()}}
</body>
抱歉,如果我的話不是很穩定,謝謝大家。
{{Form::model($user, ['method'=>'PATCH', 'action'=> ['testing@update',$user->id]])}}
或者嘗試使用“路由”而不是“動作”,要使用“路由”,您只需在更新路由中進行一些編輯即可。
Route::patch("update/{id}", array('as' => 'task-update', 'uses'=>'testing@update'));
在您看來:
{{Form::model($user, ['method'=>'PATCH', 'route'=>['task-update',$user->id]])}}
並且請遵循類命名的約定。 您的類名稱應為“ TestingController”或“ Testing”。
您可以嘗試通過添加方法欺騙
{{ method_field('PATCH') }}
在您的表單中並將表單方法更改為POST
{{ Form::model($user, ['method'=>'POST', 'action'=>['testing@update', $user->id]]) }}
將ID添加為隱藏字段
{{ Form::hidden('id', $user->id) }}
以以下方式訪問控制器中的ID
public function update(Request $Request)
{
$id = Input::get('id');
$user = T_collaborateurs_table::find($id);
$user->update(Request::all());
return redirect("index");
}
還需要相應地修改您的路線
Route::patch("update", "testing@update");
嘗試使用function update
:
return redirect()->route('index');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.