hipsterjazzbo/landlord

为 Laravel 5.2+ 提供的简单单数据库多租户解决方案

v2.0.8 2017-09-18 08:27 UTC

This package is auto-updated.

Last update: 2024-09-24 04:07:03 UTC


README

Landlord for Laravel & Lumen 5.2+

StyleCI Status
Build Status

Laravel & Lumen 5.2+ 的单数据库多租户包。

从 Landlord v1 升级? 请务必阅读 变更日志,了解需要更新什么。

安装

要开始,请要求此包

composer require hipsterjazzbo/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 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();

贡献

如果你发现了一个问题,或者有更好的方法来做某事,请随意提交一个 issue 或 pull request。