php - Laravel5.8 模型绑定(bind)路由 Arg 1 传递的必须是 Model 的实例

标签 php laravel laravel-routing laravel-5.8

Laravel 项目已本地化并运行良好。进行定制,因为用户可以将语言(区域设置)保存在数据库中,这就是问题所在。

  1. 用户能够将语言保存在数据库中。
  2. 当用户尝试编辑语言(区域设置)时,会遇到此问题。

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/

相关文章:

php - 如何创建指向另一个 PHP 页面的链接

php - MySQL:聚合和分组

php - 在 Laravel 中通过路由将变量传递到 Controller 时出现错误

php - Laravel 5 - 重定向到 HTTPS

php - Laravel 函数 Model::destroy() 中的参数太少

PHP mysql 查询返回空白屏幕

php - 如何在其他时间也获得相同帖子标题的独特 slug?

php - Laravel 5.4 验证器看不到输入字段

javascript - 将表单数据从表单的 View 传递到 Controller

php - Bootstrap 3 Modals 通过单独的 html/php 文件包含