From e5bc8ddb776f79f93ee72c0580fb2250e73f7f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 1 Oct 2022 18:01:39 +0200 Subject: [PATCH] add tenantIdColumn() method (refactor previous commit) --- src/Database/Concerns/BelongsToTenant.php | 11 ++++++++--- src/Database/Concerns/HasScopedValidationRules.php | 4 ++-- src/Database/TenantScope.php | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Database/Concerns/BelongsToTenant.php b/src/Database/Concerns/BelongsToTenant.php index 23c66332..07048a1f 100644 --- a/src/Database/Concerns/BelongsToTenant.php +++ b/src/Database/Concerns/BelongsToTenant.php @@ -14,7 +14,12 @@ trait BelongsToTenant { public function tenant() { - return $this->belongsTo(config('tenancy.tenant_model'), config('tenancy.single_db.tenant_id_column')); + return $this->belongsTo(config('tenancy.tenant_model'), static::tenantIdColumn()); + } + + public static function tenantIdColumn(): string + { + return config('tenancy.single_db.tenant_id_column'); } public static function bootBelongsToTenant(): void @@ -22,9 +27,9 @@ trait BelongsToTenant static::addGlobalScope(new TenantScope); static::creating(function ($model) { - if (! $model->getAttribute(config('tenancy.single_db.tenant_id_column')) && ! $model->relationLoaded('tenant')) { + if (! $model->getAttribute(static::tenantIdColumn()) && ! $model->relationLoaded('tenant')) { if (tenancy()->initialized) { - $model->setAttribute(config('tenancy.single_db.tenant_id_column'), tenant()->getTenantKey()); + $model->setAttribute(static::tenantIdColumn(), tenant()->getTenantKey()); $model->setRelation('tenant', tenant()); } } diff --git a/src/Database/Concerns/HasScopedValidationRules.php b/src/Database/Concerns/HasScopedValidationRules.php index 8288f2e3..7913a215 100644 --- a/src/Database/Concerns/HasScopedValidationRules.php +++ b/src/Database/Concerns/HasScopedValidationRules.php @@ -11,11 +11,11 @@ trait HasScopedValidationRules { public function unique($table, $column = 'NULL') { - return (new Unique($table, $column))->where(config('tenancy.single_db.tenant_id_column'), $this->getTenantKey()); + return (new Unique($table, $column))->where(BelongsToTenant::tenantIdColumn(), $this->getTenantKey()); } public function exists($table, $column = 'NULL') { - return (new Exists($table, $column))->where(config('tenancy.single_db.tenant_id_column'), $this->getTenantKey()); + return (new Exists($table, $column))->where(BelongsToTenant::tenantIdColumn(), $this->getTenantKey()); } } diff --git a/src/Database/TenantScope.php b/src/Database/TenantScope.php index 43214959..fdab9d70 100644 --- a/src/Database/TenantScope.php +++ b/src/Database/TenantScope.php @@ -17,7 +17,7 @@ class TenantScope implements Scope return; } - $builder->where($model->qualifyColumn(config('tenancy.single_db.tenant_id_column')), tenant()->getTenantKey()); + $builder->where($model->qualifyColumn(BelongsToTenant::tenantIdColumn()), tenant()->getTenantKey()); } public function extend(Builder $builder): void