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命令操作如下:
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
}
关于php - Laravel5.8 模型绑定(bind)路由 Arg 1 传递的必须是 Model 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56292895/