lukapeharda/multitenancy

基于子域名路由的 Laravel 单数据库多租户包。

v1.0.2 2017-01-26 07:08 UTC

This package is auto-updated.

Last update: 2024-09-17 20:45:23 UTC


README

简介

MultiTenancy 是一个基于子域名路由的 Laravel 单数据库多租户包。

安装

使用 Composer 在项目根目录中要求此包。

composer require lukapeharda/multitenancy

将服务提供者添加到 config/app.php 中的 providers 数组。

LukaPeharda\MultiTenancy\Providers\ContextServiceProvider::class,

发布配置文件并修改其参数以适应您的需求和安装。

php artisan vendor:publish --provider="LukaPeharda\MultiTenancy\Providers\ContextServiceProvider"

用法

LukaPeharda\MultiTenancy\Scopes\Contextable 特性添加到您的“租户依赖”模型中。此特性将自动加载全局范围,该范围将根据定义的租户键过滤所有查询。

除了在模型中添加特性之外,您的模型数据库模式需要将租户键作为其属性(并且很可能是其外键)。

要禁用租户全局范围,请使用 withoutGlobalScope 构建方法。

$model->withoutGlobalScope(\LukaPeharda\MultiTenancy\Scopes\TenantScope::class);

通过辅助函数 context 可获取当前租户(及其所有属性)。

// To fetch entire tenant object
$tenant = context();

// To fetch one of its attribute
$tenantId = context('id');

许可证

MultiTenancy 是开源软件,许可协议为 MIT 许可证