skillup/tenant-scope
最新稳定版本:1.0.0
Composer 安装命令:
composer require skillup/tenant-scope
包简介
Strong Global Scope package for Laravel multi-tenant systems
README 文档
README
This package provides shared tenant_id global scoping across Laravel services.
Installation
1. Install via Composer
composer require skillup/tenant-scope
If you're using it locally, add the repo to your composer.json:
"repositories": [ { "type": "path", "url": "../packages/tenant-scope" } ]
2. Publish Config (Optional)
php artisan vendor:publish --tag=config
3. Usage in Model
use TenantScope\Traits\BelongsToTenant; class User extends Model { use BelongsToTenant; protected $fillable = [ 'tenant_id', 'name', 'email' ]; }
4. Set Tenant Context on (Request, Jobs, Artisan)
Unit tests In your Background jobs , Artisan commands and Unit tests:
TenantContext::setTenantId($request->header('X-Tenant-ID'));
5. Set Tenant Context from Middleware
Once your package is installed:
- The middleware is auto-registered as
tenant.context - You can get
tenant_idfromheaderorjwt - You can use it in routes:
Route::middleware('tenant.context')->group(function () { // Get only users belongs to the tenant $users= \App\Models\User::all()->toArray(); });
How to Use in Microservices
- Include the package via Composer
- Call
TenantContext::setTenantId($id)early in the request lifecycle - Add
use BelongsToTenanton all tenant-scoped models
Indexing Best Practice
In Laravel migration:
$table->index(['tenant_id', 'email']); // Composite index with tenant_id first
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-31