1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:54:03 +00:00

add tenantIdColumn() method (refactor previous commit)

This commit is contained in:
Samuel Štancl 2022-10-01 18:01:39 +02:00
parent 24146b26e2
commit e5bc8ddb77
3 changed files with 11 additions and 6 deletions

View file

@ -14,7 +14,12 @@ trait BelongsToTenant
{ {
public function tenant() 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 public static function bootBelongsToTenant(): void
@ -22,9 +27,9 @@ trait BelongsToTenant
static::addGlobalScope(new TenantScope); static::addGlobalScope(new TenantScope);
static::creating(function ($model) { 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) { if (tenancy()->initialized) {
$model->setAttribute(config('tenancy.single_db.tenant_id_column'), tenant()->getTenantKey()); $model->setAttribute(static::tenantIdColumn(), tenant()->getTenantKey());
$model->setRelation('tenant', tenant()); $model->setRelation('tenant', tenant());
} }
} }

View file

@ -11,11 +11,11 @@ trait HasScopedValidationRules
{ {
public function unique($table, $column = 'NULL') 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') 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());
} }
} }

View file

@ -17,7 +17,7 @@ class TenantScope implements Scope
return; 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 public function extend(Builder $builder): void