kaiorocha/landlord

适用于Laravel 5.2+的简单单一数据库多租户解决方案

2.4.2 2020-05-28 21:07 UTC

This package is auto-updated.

Last update: 2024-09-29 05:36:22 UTC


README

Landlord for Laravel & Lumen 5.2+

StyleCI Status
Build Status

基于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中设置服务提供者

$app->register(HipsterJazzbo\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 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的新模型实例时,如果它们尚未设置,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异常,尽管这些行确实存在,但它们属于错误的租户。这可能令人困惑。

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();

贡献

如果您发现任何问题,或者有更好的方法来做某事,请随时提出一个问题或发起一个拉取请求。