acdphp/laravel-multitenancy
Composer 安装命令:
composer require acdphp/laravel-multitenancy
包简介
Laravel multi-tenancy model scoping and automatic tenancy assignment.
README 文档
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', ... ]; }
Per-model column name override
The tenant_ref_key config defines the column name globally. You can override it for a specific model by defining the $tenantRefKey property. This affects auto-assignment on creation and all scoping queries for that model.
use \Acdphp\Multitenancy\Traits\BelongsToTenant; class Site extends Model { use BelongsToTenant; protected string $tenantRefKey = 'org_id'; // Override the global tenant_ref_key for this model protected $fillable = [ 'org_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(...);
Custom Scoping Tenant ID
By default, the scoping uses the resolved tenant ID. You can set a custom scoping tenant ID resolver that supports a single value or an array of IDs.
use Acdphp\Multitenancy\Facades\Tenancy; // Scope to a single tenant Tenancy::setScopingTenantIdResolver(fn () => $tenantId); // Scope to multiple tenants Tenancy::setScopingTenantIdResolver(fn () => [1, 2, 3]);
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', ...);
To re-enable scoping after bypassing:
Tenancy::resetScopeBypass();
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', ...);
To re-enable auto-assignment after bypassing:
Tenancy::resetCreatingBypass();
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.
acdphp/laravel-multitenancy 适用场景与选型建议
acdphp/laravel-multitenancy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.44k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 10 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 acdphp/laravel-multitenancy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 acdphp/laravel-multitenancy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-09