问题描述
Laravel BACK PACK管理面板。我想使用匿名全局范围。这里是link。我有两个表(users,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
但是现在我想在两个表(用户,accounts_profiles)之间联接。 我知道我们将在用户模型中编写查询。
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)"
}
]
非常感谢您。
解决方法
let date = Date()
// current,here: "Sep 30,2020 at 9:27 AM"
let distantPast = Date.distantPast
// "Jan 1,1 at 12:53 AM"
let distantFuture = Date.distantFuture
// "Jan 1,4001 at 1:00 AM"
[其中“ id” = 1],此ID不明确,请检查您的$ builder。
建议使用雄辩的ORM范围