Laravel BACK PACK admin panel. Anonymous Global scopes Use

330 views Asked by At

Laravel BACK PACK admin panel. i want to use Anonymous Global scopes. here is link. I have two tables (users , accounts_profiles) in below screenshot you can see in accounts_profiles we have a column of user_id.

ACCOUNT PROFILE TABLE USER TABALE

first , let me explain you. That code I do put in User Model.

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

And that gave me that record in admin panel.( because i'm fetching only user_id "1" record) RECORDS

but now I want to join between two tables( users , accounts_profiles). I know we will write query in User Model.

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');
    });
}

but im getting that Error.

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)"
}
]

Thank you so much.

1

There are 1 answers

1
夜神夜神 On
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] ,this id is ambiguous, check your $builder .

Eloquent ORM scope is recommended