【发布时间】:2021-01-15 19:22:57
【问题描述】:
Laravel BACK PACK 管理面板。我想使用匿名全局范围。这里是link。我有两个表(用户,accounts_profiles) 在下面的屏幕截图中,您可以在 accounts_profiles 中看到我们有一列 user_id。
ACCOUNT PROFILE TABLE USER TABALE
首先,让我解释一下。 我确实将代码放入用户模型中。
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" 记录) RECORDS
但现在我想加入两个表(用户、帐户配置文件)之间。 我知道我们会在用户模型中编写查询。
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)"
}
]
非常感谢。
【问题讨论】:
-
如果这样尝试会得到什么
static::addGlobalScope('users', function (Builder $builder) use ($userId) { return $builder->with("accounts_profile_biz"); }); -
异常代码:“调用模型 [App\User] 上的未定义关系 [accounts_profile_biz]。”该错误产生。@EncangCutbray
-
你为表
accounts_profiles_biz创建模型了吗 -
也请分享您的用户模型
-
用户的模型名称是“User”,accounts_profile_biz 的模型名称是“AccountsProfileBiz”
标签: php laravel laravel-backpack global-scope