maize-tech / laravel-tenant-aware
Laravel 租户感知
Requires
- php: ^8.1
- illuminate/database: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.14.1
Requires (Dev)
- larastan/larastan: ^2.0.1
- laravel/pint: ^1.0
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.3
README
Laravel 租户感知
轻松将单数据库多租户功能集成到您的 Laravel 应用程序中。
安装
您可以通过 composer 安装此包
composer require maize-tech/laravel-tenant-aware
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="tenant-aware-config"
这是发布配置文件的内容
return [ 'tenant' => [ /* |-------------------------------------------------------------------------- | Tenant Model |-------------------------------------------------------------------------- | | Here you may specify the fully qualified class name of the tenant model. | */ 'model' => null, /* |-------------------------------------------------------------------------- | Tenant key name |-------------------------------------------------------------------------- | | Here you may specify the column name for the tenant identifier. | All models using the multi-tenant feature should include this field | in their migration. | */ 'foreign_key_name' => 'tenant_id', /* |-------------------------------------------------------------------------- | Actions |-------------------------------------------------------------------------- | | Here you may specify the list of actions used to retrieve che current | and landlord tenants. | */ 'actions' => [ 'current' => Maize\TenantAware\Actions\TenantCurrentAction::class, 'landlord' => Maize\TenantAware\Actions\TenantLandlordAction::class, 'current_or_landlord' => Maize\TenantAware\Actions\TenantCurrentOrLandlordAction::class, 'only_current' => Maize\TenantAware\Actions\TenantOnlyCurrentAction::class, ], ], 'models' => [ /* |-------------------------------------------------------------------------- | Global Models |-------------------------------------------------------------------------- | | Here you may specify the full list of global models who should return | all entities, including the ones related to the landlord tenant. | */ 'global' => [ // App\Models\Article::class, ], 'listen' => [ /* |-------------------------------------------------------------------------- | Set tenant key |-------------------------------------------------------------------------- | | Here you may specify the action invoked when creating a multi-tenant | model entity. | By default, the action sets the tenant field to the current tenant key. | */ 'creating' => Maize\TenantAware\Listeners\SetTenantKey::class, ], ], 'scope' => [ /* |-------------------------------------------------------------------------- | Scope apply |-------------------------------------------------------------------------- | | Here you may override the default scope method applied to all models | who belong to a tenant. | The default class adds a where constraint to exclude entities not related | to the current user's tenant or to the landlord tenant. | */ 'apply' => Maize\TenantAware\Scopes\ScopeTenantAware::class, /* |-------------------------------------------------------------------------- | Scope Methods |-------------------------------------------------------------------------- | | Here you may override the default scope methods used to add the | where constraints to all models who belong to a tenant. | */ 'methods' => [ Maize\TenantAware\Scopes\ScopeOrTenantWhere::class, Maize\TenantAware\Scopes\ScopeTenantWhere::class, Maize\TenantAware\Scopes\ScopeTenantAware::class, ], ], ];
使用方法
配置
在开始之前,请确保在 config/tenant-aware.php
中的 model
和 foreign_key_name
属性下填写。
此外,不要忘记定义自己的 TenantCurrentAction
和 TenantLandlordAction
动作以检索当前租户和房东租户。
例如,如果您正在使用 Spatie 的 laravel-multitenancy 包,则自定义动作可能如下所示
<?php namespace Support\TenantAware; use Spatie\Multitenancy\Models\Tenant; class TenantCurrentAction { public function __invoke(): ?Model { return Tenant::current(); } }
<?php namespace Support\TenantAware; use Maize\TenantAware\Support\Config; use Spatie\Multitenancy\Models\Tenant; class TenantLandlordAction { public function __invoke(): ?Model { $tenantModel = Config::getTenantModelName(); $landlordCode = 'landlord'; // get landlord tenant from your config, env, or wherever you defined it return app($tenantModel)::query() ->firstWhere('code', $landlordCode); } }
基本
要使用此包,请将 Maize\TenantAware\BelongsToTenant
特性添加到您想使用多租户功能的模型中。
请记住,所有您的多租户模型都应该在 config/tenant-aware.php
中的 foreign_key_name
属性下指定租户字段。
<?php namespace App\Models; use Maize\TenantAware\BelongsToTenant; class User extends Model { use BelongsToTenant; protected $fillable = [ 'fist_name', 'last_name', 'email', 'tenant_id', ]; }
完成后,当查询任何多租户模型时,您将只检索与您同租户的实体。
假设,例如,我们数据库中有一个用户列表,当前认证用户是租户 2
的一部分
当查询用户模型时,您将只得到前三个用户
use App\Models\User; User::all()->modelKeys(); // returns [1, 2, 3]
查询全局实体
有一个模型需要处理全局和租户相关实体吗?没关系!
您只需将模型类名添加到 config/tenant-aware.php
中的 models.global
属性下。同时,确保您的房东租户在自定义 TenantLandlordAction
动作类中返回!
'models' => [ 'global' => [ App\Models\Course::class, ], ],
假设您有一个具有全局课程和每个租户自定义课程的学习多租户平台。
这就是您在查询租户 2
下的用户时从 Course 模型得到的
use App\Models\Course; Course::all()->modelKeys(); // returns [1, 2, 3]
测试
composer test
更新日志
有关最近更改的更多信息,请参阅 更新日志
贡献
有关详细信息,请参阅 贡献
安全漏洞
请查看 我们的安全策略 了解如何报告安全漏洞。
致谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件