stefanbauer / landlord-extended
Laravel 5.2+ 的简单单数据库多租户解决方案
Requires
- php: >=5.6.0
- illuminate/support: 5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0
Requires (Dev)
- laravel/framework: 5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0
- mockery/mockery: ~0.9|~1.0|~1.1|~1.2|~1.3|~1.4
- phpunit/phpunit: 5.5.*|6.0.*|7.0.*|8.0.*|9.0.*
README
此包基于 HipsterJazzbo/Landlord,这是一个适用于 Laravel & Lumen 5.2+ 的单数据库多租户包。虽然保持相同,但维护依赖关系。
安装
要开始使用,请通过
composer require stefanbauer/landlord-extended
Laravel
版本 5.5+
Laravel >= 5.5 使用包自动发现,因此不需要您手动添加 ServiceProvider。如果还需要,以下是方法
版本 5.4 及以下
在 config/app.php 中添加 ServiceProvider
'providers' => [ ... StefanBauer\Landlord\LandlordServiceProvider::class, ],
注册 Facade,如果您愿意
'aliases' => [ ... 'Landlord' => StefanBauer\Landlord\Facades\Landlord::class, ],
您还可以发布配置文件
php artisan vendor:publish --provider="StefanBauer\Landlord\LandlordServiceProvider"
并设置您的 default_tenant_columns 设置,如果您有全局默认设置。LandLord 将使用此设置来范围没有设置 $tenantColumns 属性的模型。
Lumen
您需要在您的 bootstrap/app.php 中设置服务提供者
$app->register(StefanBauer\Landlord\LandlordServiceProvider::class);
并确保您已取消注释 $app->withEloquent()。
用法
此包假设您在所有租户范围表中至少有一个列引用了每行属于哪个租户。
例如,您可能有一个 companies 表,以及其他包含 company_id 列的其他表。
添加和删除租户
重要提示: Landlord 是无状态的。这意味着当您调用
addTenant()时,它只会范围到 当前请求。请确保您以这种方式添加租户,即在每次请求中,并且在需要范围模型之前,如中间件或作为 OAuth 等无状态认证方法的一部分。
您可以通过调用 addTenant() 告诉 Landlord 自动按给定租户范围,无论是从 Landlord Facade 还是通过注入 TenantManager() 实例。
您可以通过传递租户列和 ID
Landlord::addTenant('tenant_id', 1);
或租户模型的实例
$tenant = Tenant::find(1); Landlord::addTenant($tenant);
如果传递模型实例,Landlord 将使用 Eloquent 的 getForeignKey() 方法来决定租户列名。
您可以添加任意数量的租户,但是 Landlord 一次只允许 一种 类型的租户。
要删除租户并停止通过它范围,只需调用 removeTenant()
Landlord::removeTenant('tenant_id'); // Or you can again pass a Model instance: $tenant = Tenant::find(1); Landlord::removeTenant($tenant);
您还可以检查 Landlord 当前是否正在通过给定租户范围
// As you would expect by now, $tenant can be either a string column name or a Model instance Landlord::hasTenant($tenant);
并且如果您需要,您可以检索 Landlord 的租户
// $tenants is a Laravel Collection object, in the format 'tenant_id' => 1 $tenants = Landlord::getTenants();
设置您的模型
要设置一个自动范围的模型,只需使用 BelongsToTenants 特性
use Illuminate\Database\Eloquent\Model; use StefanBauer\Landlord\BelongsToTenants; class ExampleModel extends Model { use BelongsToTenants; }
如果您想覆盖特定模型适用的租户,可以设置 $tenantColumns 属性
use Illuminate\Database\Eloquent\Model; use StefanBauer\Landlord\BelongsToTenants; class ExampleModel extends Model { use BelongsToTenants; public $tenantColumns = ['tenant_id']; }
创建新的租户范围模型
当您创建一个使用 BelongsToTenants 的模型的实例时,Landlord 将自动添加任何适用的租户 ID,如果尚未设置
// 'tenant_id' will automatically be set by Landlord $model = ExampleModel::create(['name' => 'whatever']);
查询租户范围模型
在添加租户后,针对使用 BelongsToTenant 的模型的查询将自动范围
// This will only include Models belonging to the current tenant(s) ExampleModel::all(); // This will fail with a ModelNotFoundForTenantException if it belongs to the wrong tenant ExampleModel::find(2);
注意: 当你开发多租户应用程序时,有时你会困惑为什么你会持续接收到对实际存在的行的
ModelNotFound异常,因为它们属于错误的租户。房东会捕获这些异常,并将它们重新抛出为
ModelNotFoundForTenantException,以帮助你解决问题 :)
如果你需要跨所有租户进行查询,可以使用 allTenants()
// Will include results from ALL tenants, just for this query ExampleModel::allTenants()->get()
在底层,Landlord 使用 Laravel 的 匿名全局作用域。这意味着如果你正在同时针对多个租户进行范围限定,并且你想要在单个查询中排除其中一个,你可以这样做
// Will not scope by 'tenant_id', but will continue to scope by any other tenants that have been set ExampleModel::withoutGlobalScope('tenant_id')->get();
贡献
如果你发现了一个问题,或者有更好的方法来做某事,请随时打开一个问题或拉取请求。