bissolli/landlord

Laravel 的简单、单数据库多租户解决方案

3.0.3 2018-02-17 21:21 UTC

This package is auto-updated.

Last update: 2024-08-28 01:40:54 UTC


README

Laravel 的单数据库多租户包

Build Status Latest Stable Version Total Downloads License

基于 HipsterJazzbo/Landlord.

从 Landlord v1 升级? 确保阅读 变更日志,以了解需要更新哪些内容。

安装

要开始使用,请要求此包

composer require bissolli/laravel-landlord

Laravel

config/app.php 中添加 ServiceProvider

    'providers' => [
        ...
        Bissolli\Landlord\LandlordServiceProvider::class,
    ],

如果您想,可以注册 Facade

    'aliases' => [
        ...
        'Landlord'   => Bissolli\Landlord\Facades\Landlord::class,
    ],

您还可以发布配置文件

php artisan vendor:publish --provider="Bissolli\Landlord\LandlordServiceProvider"

并设置您的 default_tenant_columns 设置,如果您有一个全局默认值。LandLord 将使用此设置来范围没有设置 $tenantColumns 属性的模型。

Lumen

您需要在您的 bootstrap/app.php 中设置 service provider

$app->register(Bissolli\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 Bissolli\Landlord\BelongsToTenants;

class ExampleModel extends Model
{
    use BelongsToTenants;
}

如果您想为特定模型覆盖适用的租户,可以设置 $tenantColumns 属性

use Illuminate\Database\Eloquent\Model;
use Bissolli\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();

贡献

如果你发现了一个问题,或者有更好的做事方式,请随意打开一个 issue 或 pull request。