Php Laravel BACK PACK管理面板。匿名全局作用域使用
Laravel BACK PACK管理面板。我想使用匿名全局作用域。这是。我有两个表(用户、帐户和个人资料) 在下面的屏幕截图中,您可以在accounts\u profiles中看到我们有一列用户id 首先,让我解释一下。 我在用户模型中输入的代码Php Laravel BACK PACK管理面板。匿名全局作用域使用,php,laravel,backpack-for-laravel,global-scope,Php,Laravel,Backpack For Laravel,Global Scope,Laravel BACK PACK管理面板。我想使用匿名全局作用域。这是。我有两个表(用户、帐户和个人资料) 在下面的屏幕截图中,您可以在accounts\u profiles中看到我们有一列用户id 首先,让我解释一下。 我在用户模型中输入的代码 protected static function boot(){ parent::boot(); $userId = 1; static::addGlobalScope('users', function (Builde
protected static function boot(){
parent::boot();
$userId = 1;
static::addGlobalScope('users', function (Builder $builder) use ($userId) {
return $builder->where('users.id', $userId);
});
}
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');
});
}
这给了我管理面板中的记录(因为我只获取用户id“1”记录)
但现在我想连接两个表(用户、帐户和配置文件)。
我知道我们将在用户模型中编写查询
protected static function boot(){
parent::boot();
$userId = 1;
static::addGlobalScope('users', function (Builder $builder) use ($userId) {
return $builder->where('users.id', $userId);
});
}
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)"
}
]
非常感谢你
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
[其中“id”=1],此id不明确,请检查$builder
建议使用雄辩的ORM范围如果这样尝试,您会得到什么
static::addGlobalScope('users',function(Builder$Builder)use($userId){return$Builder->with(“accounts_profile_biz”);})代码>异常代码:“调用模型[App\User]上未定义的关系[accounts\u profile\u biz]”产生该错误。@EncangCutbrayDid您为表accounts\u profiles\u biz
创建模型请将您的用户模型共享到用户的模型名为“User”,账户的模型名为“accounts\u profilebiz”返回$builder->where('users.id',$userId);但当我这样做时,它会给我正确的数据。但这在join中造成了问题。