Laravel 7资源模型绑定,编辑页面中没有任何信息



在Laravel 7中,我做了以下操作:

php artisan make:controller ClientGroupController --resource --model=ClientGroup

我修改了ClientGroupController中的edit函数,如下所示:

public function edit(ClientGroup $clientGroup)
{
return view('extranet.groups.create_modal_form', compact('clientGroup'));
}

我还添加了这个路由资源:Route::resource('groups', 'ClientGroupController');

视图中的dd($clientGroup)(访问页面时http://127.0.0.1:8000/groups/2/edit)不产生当前记录的任何数据(空白ClientGroup对象(。

我错过了一步吗?为什么$clientGroup->id在我的视图中返回null(id是client_group表的主键(。

public function edit($id)
{
$clientGroup = ClientGroup::find($id);
return view('extranet.groups.create_modal_form', compact('clientGroup'));
}

我在定义如下的资源路由时遇到了类似的问题:

Route::resource('others', OtherServiceController::class);

但我的型号是OtherService

这是我的编辑和更新功能

public function edit(OtherService $otherService)
{
return view('others.master-edit', compact('otherService'));
}
public function update(OtherServiceRequest $request, OtherService $otherService)
{
$otherService->update($request->validated());
return redirect()->route('others.index')->withToastSuccess('Success Update Data');
}

但由于资源名称和模型名称不相同,此代码引发了有关缺少必需参数的错误。因此,我的解决方案是更改资源路线以匹配模型。

Route::resource('otherService', OtherServiceController::class);

也许你可以阅读这个问题,标记的答案告诉关于覆盖

最新更新