Laravel5.8 传递的模型绑定路由参数 1 必须是模型的实例
Laravel5.8 Model Binding Route Arg 1 passed must be an instance of Model
Laravel 项目已通过本地化实施并运行良好。进行自定义,因为用户可以在数据库中保存语言(语言环境),问题就来了。
- 用户可以在数据库中保存语言。
- 并且当用户尝试编辑语言(语言环境)时出现此问题。
Argument 1 passed to App\Http\Controllers\CustomizeController::edit() must be an instance of App\Model\Customize, string given
在CustomizeController.php
public function index()
{
$data = array(
'title' =>'Customize',
'heading' =>'List',
'customize' => Customize::where(['user_id' => Auth::user()->id])->first(),
);
if ($data['customize'])
{
return redirect()->route('customize.edit', ['locale' => app()->getLocale(), 'customize' => $data['customize']]); // Redirect to Edit Route If Language available in DB
}
return view('Customize.index')->with($data);
}
route:list 命令 O/P 如下:
GET|HEAD | {locale}/customize/{customize}/edit | customize.edit | App\Http\Controllers\CustomizeController@edit | web,setlocale,auth
甚至尝试从 blade 硬编码为:
<a href="{{ url(app()->getLocale().'/customize/1/edit') }}">
<button type="button" class="btn btn-warning">Edit</button>
</a>
完成项目available here
错误非常简单。
使用路由参数调用 edit
方法,后跟您定义的要从容器中获取的对象。
由于您将 customize
参数传递给路由,因此您的方法参数应该定义如下:
public function edit(string $customize, App\Model\Customize $customizeModel)
{
$customizeModel
->whereUserId(Auth::user()->id)
->update(['customize' => $customize]);
$return response(); // whatever you need
}
Laravel 项目已通过本地化实施并运行良好。进行自定义,因为用户可以在数据库中保存语言(语言环境),问题就来了。
- 用户可以在数据库中保存语言。
- 并且当用户尝试编辑语言(语言环境)时出现此问题。
Argument 1 passed to App\Http\Controllers\CustomizeController::edit() must be an instance of App\Model\Customize, string given
在CustomizeController.php
public function index()
{
$data = array(
'title' =>'Customize',
'heading' =>'List',
'customize' => Customize::where(['user_id' => Auth::user()->id])->first(),
);
if ($data['customize'])
{
return redirect()->route('customize.edit', ['locale' => app()->getLocale(), 'customize' => $data['customize']]); // Redirect to Edit Route If Language available in DB
}
return view('Customize.index')->with($data);
}
route:list 命令 O/P 如下:
GET|HEAD | {locale}/customize/{customize}/edit | customize.edit | App\Http\Controllers\CustomizeController@edit | web,setlocale,auth
甚至尝试从 blade 硬编码为:
<a href="{{ url(app()->getLocale().'/customize/1/edit') }}">
<button type="button" class="btn btn-warning">Edit</button>
</a>
完成项目available here
错误非常简单。
使用路由参数调用 edit
方法,后跟您定义的要从容器中获取的对象。
由于您将 customize
参数传递给路由,因此您的方法参数应该定义如下:
public function edit(string $customize, App\Model\Customize $customizeModel)
{
$customizeModel
->whereUserId(Auth::user()->id)
->update(['customize' => $customize]);
$return response(); // whatever you need
}