acdphp / laravel-multitenancy
Laravel multi-tenancy model scoping and automatic tenancy assignment.
Installs: 1 384
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 0
Forks: 1
Open Issues: 0
pkg:composer/acdphp/laravel-multitenancy
Requires
- php: ^8.0
- illuminate/database: ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0
- illuminate/support: ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0
Requires (Dev)
- ekino/phpstan-banned-code: ^1.0 || ^2.0 || ^3.0
- larastan/larastan: ^1.0 || ^2.0 || ^3.0
- laravel/pint: ^1.0
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0 || ^10.0
- phpstan/phpstan: ^1.10 || ^2.0
- phpunit/phpunit: ^9.0 || ^10.0 || ^11.0
README
Laravel multi-tenancy model scoping and automatic tenancy assignment.
Installation
composer require acdphp/laravel-multitenancy
Configuration
Publish config
php artisan vendor:publish --provider="Acdphp\Multitenancy\TenancyServiceProvider"
Change the column name to look for tenancy in models.
'tenant_ref_key' => 'company_id',
By default, the tenant id is automatically resolved using the authenticated user's tenant ref key defined above. You can disable this and define your own resolver logic in AppServiceProvider or any service provider.
'auto_resolve_tenant_id' => true,
By default, the tenant id is automatically assigned during creation using the resolved tenancy defined above. You can disable this and manually set tenant id as you normally would.
'auto_assign_tenant_id' => true,
Example Usage
use \Acdphp\Multitenancy\Traits\BelongsToTenant; class Site extends Model { use BelongsToTenant; protected $fillable = [ 'company_id', ... ]; }
Scoping from parent relationship
use \Acdphp\Multitenancy\Traits\BelongsToTenant; class Product extends Model { use BelongsToTenant; protected $fillable = [ 'site_id', ... ]; protected string $scopeTenancyFromRelation = 'site'; // Define to scope from parent model public function site(): BelongsTo { return $this->belongsTo(Site::class); } }
Manually setting the tenant
In the registration, for example, tenancy isn't set because it's a non-authenticated endpoint. The tenant has to be manually assigned.
use Acdphp\Multitenancy\Facades\Tenancy; // Create a company and set it as tenant $company = Company::create(...); Tenancy::setTenantIdResolver(fn () => $company->id); // Then proceed to create a user User::create(...);
Bypassing Scope
Sometimes, it's needed to bypass scoping when accessing a model that belongs to a tenant.
use Acdphp\Multitenancy\Facades\Tenancy; Tenancy::bypassScope();
Or by using the middleware.
Route::middleware(['tenancy.scope.bypass'])->get('/resources/all', ...);
Bypassing Automatic Tenancy Assignment
It's also possible to bypass auto-tenancy assignments.
use Acdphp\Multitenancy\Facades\Tenancy; Tenancy::bypassCreating();
Or by using the middleware.
Route::middleware(['tenancy.creating.bypass'])->post('your-route', ...);
Testing
composer test # with docker-compose docker-compose run --rm test-php84
Linting and static analysis
composer lint
# with docker-compose
docker-compose run --rm lint
Lint Fix
composer lint-fix
# with docker-compose
docker-compose run --rm lint-fix
License
The MIT License (MIT). Please see License File for more information.