pong106/landlord

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

维护者

详细信息

github.com/pong106/Landlord

源代码

安装: 290

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 138

3.0.0 2020-04-20 04:17 UTC

This package is auto-updated.

Last update: 2024-08-29 05:05:57 UTC


README

Landlord for Laravel & Lumen 5.2+

StyleCI Status
Build Status

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

从 Landlord v1 升级? 请确保阅读 变更日志,查看需要更新的内容。

安装

要开始,请要求此包

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

如果您传递 Model 实例,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();

贡献

如果您发现了一个问题,或者有更好的实现方式,请随时提交问题或拉取请求。