1

Laravel BACK PACK 管理面板。我想使用匿名全局范围。这是链接。我在下面的屏幕截图中有两个表(用户、帐户配置文件),您可以在帐户配置文件中看到我们有一列 user_id。

帐户资料表 用户表

首先,让我解释一下。我确实放在用户模型中的代码。

protected static function boot(){
    parent::boot();
    $userId = 1;
    static::addGlobalScope('users', function (Builder $builder) use ($userId) {
      return  $builder->where('users.id', $userId);
    });
}

这给了我管理面板中的记录。(因为我只获取 user_id "1" 记录) 记录

但现在我想加入两个表(用户、帐户配置文件)之间。我知道我们将在用户模型中编写查询。

protected static function boot(){
    parent::boot();
    $userId = 1;
    static::addGlobalScope('users', function (Builder $builder) use ($userId) {
      return  $builder->join("accounts_profiles_biz", 'users.id', '=', 'accounts_profiles_biz.user_id');
    });
}

但我得到了那个错误。

response_message: [
{
code: 9997,
message: "SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference "id" is ambiguous LINE 1: ...s"."id" = "accounts_profiles_biz"."user_id" where "id" = $1 ... ^ (SQL: select * from "users" inner join "accounts_profiles_biz" on "users"."id" = "accounts_profiles_biz"."user_id" where "id" = 1 and "users"."deleted_at" is null limit 1), File: D:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php, Line: 669",

Exception Code: "SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference "id" is ambiguous LINE 1: ...s"."id" = "accounts_profiles_biz"."user_id" where "id" = $1 ... ^ (SQL: select * from "users" inner join "accounts_profiles_biz" on "users"."id" = "accounts_profiles_biz"."user_id" where "id" = 1 and "users"."deleted_at" is null limit 1)"
}
]

太感谢了。

4

1 回答 1

-1
select * from "users" inner join "accounts_profiles_biz" on "users"."id" = "accounts_profiles_biz"."user_id" where "id" = 1 and "users"."deleted_at" is null limit 1

[where "id"=1] ,这个 id 不明确,检查你的 $builder 。

推荐使用 Eloquent ORM 范围

于 2020-09-30T07:33:00.860 回答