Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: run-tests

on:
push:
branches: [master]
branches: [main]
pull_request:
branches: [master]
branches: [main]

jobs:
test:
Expand Down
7 changes: 7 additions & 0 deletions config/tenantable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
12 changes: 11 additions & 1 deletion src/Models/TenantScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down