skillup/tenant-scope 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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_id from header or jwt
  • 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 BelongsToTenant on 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

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-31