hatchyu/laravel-tenancy
Composer 安装命令:
composer require hatchyu/laravel-tenancy
包简介
Flexible, resolver-based multi-tenancy for Laravel (owner, organization, branch, subdomain, token-based).
README 文档
README
A flexible, resolver-based multi-tenancy package for Laravel.
Features
- Hierarchical Tenancy: Organization → Branch → Owner.
- Context-Aware: Resolves tenants from Request (Subdomain, Header, Route, Auth).
- Security: Strict validation (e.g., Branch must belong to Organization).
- Flexible Auth: Custom tenant resolution logic via
ProvidesTenantinterface. - Bypass: System-level bypass for super-admins.
- Lightweight: Runtime resolution, no complex model inheritance.
Installation
composer require hatchyu/laravel-tenancy
php artisan vendor:publish --tag=tenancy-config
Core Concepts
this package models tenancy as a hierarchy:
- Organization: The top-level tenant (e.g., "Acme Corp").
- Branch: A sub-unit of an organization (e.g., "New York Office").
- Owner: The user who owns specific data (optional).
Tenants are resolved per request and stored in the TenantContext.
[!IMPORTANT] If no tenant is resolved (and no bypass is active), the
BelongsToTenantscope is NOT applied, effectively showing "public" data. Ensure your routes are protected by theResolveTenancymiddleware to enforce resolution for tenant-specific data.
Usage
1. Models
Add the BelongsToTenant trait to your models:
use Illuminate\Database\Eloquent\Model;
use Hatchyu\Tenancy\Traits\BelongsToTenant;
use Hatchyu\Tenancy\Enums\TenantType;
class Invoice extends Model
{
use BelongsToTenant;
protected static function tenantType(): TenantType
{
return TenantType::Organization; // or TenantType::Branch
}
protected static function tenantColumn(): string
{
return 'organization_id';
}
}
2. Middleware
Register the middleware to resolve tenants on incoming requests:
// In bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
// 1. Global (Applies to everything)
$middleware->append(\Hatchyu\Tenancy\Middleware\ResolveTenancy::class);
// OR 2. Register Alias for Route Groups
$middleware->alias([
'tenancy' => \Hatchyu\Tenancy\Middleware\ResolveTenancy::class,
]);
});
Then apply to routes:
Route::middleware('tenancy')->group(function () {
Route::get('/dashboard', ...);
});
3. Facade
You can use the Tenancy facade to access the current context:
use Hatchyu\Tenancy\Facades\Tenancy;
use Hatchyu\Tenancy\Enums\TenantType;
// Get current Tenant IDs
$orgId = Tenancy::get(TenantType::Organization);
$branchId = Tenancy::get(TenantType::Branch);
// Manually set context (e.g., in tests or jobs)
Tenancy::set(TenantType::Organization, 1);
Configuration & Resolvers
Configure resolvers in config/tenancy.php. The order matters!
Available Resolvers
- SubdomainOrganizationResolver: Extracts org from subdomain (e.g.,
acme.app.com). - HeaderOrganizationResolver: Reads
X-Organization-IDheader. - AuthOrganizationResolver: Uses
Auth::user()->organization_id(orProvidesTenantinterface). - RouteBranchResolver: Resolves branch from route params (strictly verifies org ownership).
- AuthBranchResolver: Uses
Auth::user()->branch_id(strictly verifies org ownership).
Flexible Auth Resolution
If your User model implies tenancy differently (e.g. via session or relationships), implement ProvidesTenant:
use Hatchyu\Tenancy\Contracts\ProvidesTenant;
use Hatchyu\Tenancy\Enums\TenantType;
class User extends Authenticatable implements ProvidesTenant
{
public function getTenantId(TenantType $type): int|string|null
{
return match($type) {
TenantType::Organization => $this->current_org_id,
TenantType::Branch => session('active_branch_id'),
default => null,
};
}
}
The resolvers will prioritize this interface over standard properties.
Queues & Jobs
This package resolves tenancy runtime per request. Background jobs do not automatically inherit this context. To support tenancy in queues, pass the tenant ID to the job and manually set it:
public function __construct(protected int $organizationId) {}
public function handle()
{
Tenancy::set(TenantType::Organization, $this->organizationId);
// ... run logic
}
Super-admin (platform owner)
Add bypass:
use Hatchyu\Tenancy\Contracts\TenancyBypass;
class PlatformTenancyBypass implements TenancyBypass
{
public function shouldBypass(): bool
{
return auth()->user()?->is_platform_admin ?? false;
}
}
Config:
'bypass' => PlatformTenancyBypass::class,
Philosophy
Tenancy is a runtime concern, not a model concern. This package resolves tenants before queries run and applies isolation automatically via global scopes.
This design:
- Scales to enterprise
- Fits Hatchyu code generation
- Avoids coupling
- Is extensible without inheritance hell
hatchyu/laravel-tenancy 适用场景与选型建议
hatchyu/laravel-tenancy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hatchyu/laravel-tenancy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hatchyu/laravel-tenancy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-18