VictorMacedo /landlord
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|^9.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|^9.0
- mockery/mockery: ~0.9
- phpunit/phpunit: 5.5.*
README
基于HipsterJazzbo/Landlord,一个用于Laravel & Lumen 5.2+的单数据库多租户软件包。
安装
要开始使用,请安装此软件包
composer require victormacedo/landlord
Laravel
在config/app.php
中添加ServiceProvider
'providers' => [ ... HipsterJazzbo\Landlord\LandlordServiceProvider::class, ],
如果你愿意,注册Facade
'aliases' => [ ... 'Landlord' => HipsterJazzbo\Landlord\Facades\Landlord::class, ],
你也可以发布配置文件
php artisan vendor:publish --provider="HipsterJazzbo\Landlord\LandlordServiceProvider"
并设置你的default_tenant_columns
设置,如果你有一个应用级的默认值。LandLord将使用此设置来限制没有设置$tenantColumns
属性的模型。
Lumen
你需要在bootstrap/app.php
中设置ServiceProvider
$app->register(HipsterJazzbo\Landlord\LandlordServiceProvider::class);
并确保你已经取消注释了$app->withEloquent()
。
使用
此软件包假定在你的所有租户作用域表中至少有一个列引用了哪一行属于哪个租户。
例如,你可能有一个companies
表,以及一些其他表,这些表都有一个company_id
列。
添加和删除租户
重要提示:Landlord是无状态的。这意味着当你调用
addTenant()
时,它只会作用在当前请求上。确保你以这种方式添加租户,使其在每次请求时发生,并在需要模型作用域之前,如中间件或作为无状态认证方法(如OAuth)的一部分。
你可以通过调用addTenant()
来告诉Landlord自动根据特定的租户进行作用域,无论是从Landlord
外观,还是通过注入一个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 HipsterJazzbo\Landlord\BelongsToTenants; class ExampleModel extends Model { use BelongsToTenants; }
如果你希望覆盖特定模型的租户,你可以设置$tenantColumns
属性
use Illuminate\Database\Eloquent\Model; use HipsterJazzbo\Landlord\BelongsToTenants; class ExampleModel extends Model { use BelongsToTenants; public $tenantColumns = ['tenant_id']; }
创建新的租户作用域模型
当你创建一个使用 BelongsToTenants
的模型的新实例时,房东会自动添加任何适用的租户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
异常,因为它们属于错误的租户。Landlord 会捕获这些异常,并将它们重新抛出为
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();
贡献
如果您发现了一个问题,或者有更好的实现方法,请随时提交一个 issue 或 pull request。