Download the PHP package sbine/simple-tenancy without Composer
On this page you can find all versions of the php package sbine/simple-tenancy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package simple-tenancy
Simple Laravel Multi-Tenancy
Simple Tenancy adds automatic multi-tenant support to Eloquent models stored in a shared database.
It requires zero configuration in most cases, relying on established Laravel conventions and a single column on each table.
How it works
Under the hood, it has only 4 components:
Tenant
: Keeps track of the current userHasTenancy
: A trait for models belonging to the tenant, which registers:TenancyScope
: A global scope limiting all the model's queries to the current tenantTenancyObserver
: An observer which sets the current tenant column/identifier on save
By default, the tenant is Laravel's Auth::user()
, and all tenancy checks are disabled when no one is authenticated.
Installation
-
Install using Composer:
-
Add the
HasTenancy
trait to all models belonging to the tenant: - Ensure a
user_id
column exists on the table of every model using the trait.
Customizing the tenant column/ID
If needed, you can customize the name of the tenant column or identifier by extending the Tenant class and binding it into the container:
Customizing tenancy behavior
By default, if no user is authenticated tenancy will be completely disabled. This is by design so the library doesn't interfere with testing, seeding, and other unauthenticated model usage.
To change the tenancy behavior in any way, you can override the Tenant binding in the application container.
For example, to prevent all querying and saving of tenant models when no one is authenticated:
To allow overriding tenancy checks based on a policy or method, pass a callback returning true
or false
:
For complete control over tenant behavior, you can bind your own Tenant implementation:
Disabling tenancy
For convenience, a SuperAdmin class is provided which you can bind at any time to disable tenant checks: