diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index e573a9c..69c756c 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -2,9 +2,9 @@ name: run-tests on: push: - branches: [master] + branches: [main] pull_request: - branches: [master] + branches: [main] jobs: test: diff --git a/config/tenantable.php b/config/tenantable.php index 02b1bf7..813e0b9 100644 --- a/config/tenantable.php +++ b/config/tenantable.php @@ -8,6 +8,13 @@ 'related_tenant_column' => env('TENANTABLE_RELATED_TENANT_COLUMN', 'tenant_id'), + /** + * Allow the `tenant_id` to be null. + * + * If the entry does have null tenant_id, it will be considered as a global entry. + */ + 'allow_nullable_tenant' => env('ALLOW_NULLABLE_TENANT', true), + /** * The base model for tenant. */ diff --git a/src/Models/TenantScope.php b/src/Models/TenantScope.php index ba5451b..47eec83 100644 --- a/src/Models/TenantScope.php +++ b/src/Models/TenantScope.php @@ -10,7 +10,17 @@ class TenantScope implements Scope { public function apply(Builder $builder, Model $model) { - $builder->where($model->qualifyColumn(config('tenantable.related_tenant_column')), tenant()->key()); + $column = $model->qualifyColumn(config('tenantable.related_tenant_column')); + $tenantKey = tenant()->key(); + + if (config('tenantable.allow_nullable_tenant')) { + $builder->where(function (Builder $query) use ($column, $tenantKey) { + $query->whereNull($column) + ->orWhere($column, $tenantKey); + }); + } else { + $builder->where($column, $tenantKey); + } } public function extend(Builder $builder)