Skip to content

Commit dc7b041

Browse files
authored
Merge pull request #23 from BinarCode/2.4.0
2.4.0
2 parents c7213de + 131c30c commit dc7b041

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

.github/workflows/run-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: run-tests
22

33
on:
44
push:
5-
branches: [master]
5+
branches: [main]
66
pull_request:
7-
branches: [master]
7+
branches: [main]
88

99
jobs:
1010
test:

config/tenantable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
'related_tenant_column' => env('TENANTABLE_RELATED_TENANT_COLUMN', 'tenant_id'),
1010

11+
/**
12+
* Allow the `tenant_id` to be null.
13+
*
14+
* If the entry does have null tenant_id, it will be considered as a global entry.
15+
*/
16+
'allow_nullable_tenant' => env('ALLOW_NULLABLE_TENANT', true),
17+
1118
/**
1219
* The base model for tenant.
1320
*/

src/Models/TenantScope.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ class TenantScope implements Scope
1010
{
1111
public function apply(Builder $builder, Model $model)
1212
{
13-
$builder->where($model->qualifyColumn(config('tenantable.related_tenant_column')), tenant()->key());
13+
$column = $model->qualifyColumn(config('tenantable.related_tenant_column'));
14+
$tenantKey = tenant()->key();
15+
16+
if (config('tenantable.allow_nullable_tenant')) {
17+
$builder->where(function (Builder $query) use ($column, $tenantKey) {
18+
$query->whereNull($column)
19+
->orWhere($column, $tenantKey);
20+
});
21+
} else {
22+
$builder->where($column, $tenantKey);
23+
}
1424
}
1525

1626
public function extend(Builder $builder)

0 commit comments

Comments
 (0)