1

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.

  • What do you get if try like this ```static::addGlobalScope('users', function (Builder $builder) use ($userId) { return $builder->with("accounts_profile_biz"); });``` – Encang Cutbray Sep 30 '20 at 06:56
  • Exception Code: "Call to undefined relationship [accounts_profile_biz] on model [App\User]." That error produce.@EncangCutbray – Jay Sep 30 '20 at 07:00
  • Did you create model for table ```accounts_profiles_biz``` – Encang Cutbray Sep 30 '20 at 07:04
  • Please share your user model too – Encang Cutbray Sep 30 '20 at 07:11
  • model name for user is "User" and for accounts_profile_biz "AccountsProfileBiz" – Jay Sep 30 '20 at 07:14
  • Why are you trying to apply a join inside a global scope? This functionality should be configured via a [relationship](https://laravel.com/docs/8.x/eloquent-relationships) – Wesley Smith Sep 30 '20 at 17:42
  • For what its worth, you could probably fix the Ambiguous issue by using something like `$builder->select('"accounts_profiles_biz", 'users.id', ...// other columns etc...`.... Maybe, but this would be very odd behavior IMHO. I would just use a normal relationship and set the model's `$with` property like `protected $with = ['account_profiles'];` etc to make it always load that relationship with the model. – Wesley Smith Sep 30 '20 at 17:49

1 Answers1

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

Eloquent ORM scope is recommended

  • return $builder->where('users.id', $userId); but when I do that , it give me right data. but that is creating problem in join. – Jay Sep 30 '20 at 07:51