[英]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.