A single DB multitenancy package for Laravel based on subdomain routing.
MultiTenancy is a Laravel single DB multitenancy package based on subdomain routing.
Require this package, with Composer, in the root directory of your project.
composer require lukapeharda/multitenancy
Add the service provider to
config/app.php in the
Publish the config file and modify its params to fit your needs and installation.
php artisan vendor:publish --provider="LukaPeharda\MultiTenancy\Providers\ContextServiceProvider"
LukaPeharda\MultiTenancy\Scopes\Contextable trait to your models that are "tenant" dependable. This trait will automatically load global scope which will filter all your queries by defined tenant key.
Beside adding trait to your models, your model DB schema needs to have the tenant key as its attribute (and most likely as its foreign key).
To disable tenant global scope use
withoutGlobalScope builder method:
Fetching the current tenant (and all of its attributes) is available through helper function
// To fetch entire tenant object $tenant = context(); // To fetch one of its attribute $tenantId = context('id');
MultiTenancy is open-sourced software licensed under the MIT license