承接 acdphp/laravel-multitenancy 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

acdphp/laravel-multitenancy

最新稳定版本:v2.4.2

Composer 安装命令:

composer require acdphp/laravel-multitenancy

包简介

Laravel multi-tenancy model scoping and automatic tenancy assignment.

README 文档

README

Latest Stable Version

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.

统计信息

  • 总下载量: 1.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-09