dersam/laravel-multitenant 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dersam/laravel-multitenant

Composer 安装命令:

composer require dersam/laravel-multitenant

包简介

Multitenant service provider for laravel

README 文档

README

Simple multitenant service provider for Laravel. Individual tenants will be given their own database schema. It expects a separate "core" schema for managing application-wide settings.

This does not support multitenancy via owner columns, and never will.

Installation

Install the package: composer require dersam/laravel-multitenant

Run the migration to create the tenants database in your core database. php artisan migrate

Deploy the config. php artisan vendor:publish --provider="Dersam\Multitenant\ServiceProvider"

Add a "dummy" config to config/database.php It should point to the database you want to contain your tenant schemas, but do not specify a database. This will be set dynamically.

'connections' => [
    // The core database that contains global application tables.
    'core' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
    // A "dummy" database that will be pointed to the correct tenant on the fly.
    'tenant' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
    ],
],

Usage

The service provider will be automatically registered - it can be injected with Dersam\Multitenant\TenantSwitcher. Retrieve your Tenant model, and pass that to switch the tenant connection to your new database. In this example, it is assumed that the User model has a tenant_id column, but this package leaves it up to the developer to decide where this comes from.

$tenant = Tenant::find($user->tenant_id);
if ($tenant === null) {
    return redirect('/');
}
$this->tenantSwitcher->switchGlobalTenant($tenant);

You can specify that certain models always use the tenant connection with the Dersam\Multitenant\IsTenantModel trait.

namespace App\Models\Tenant;

use Dersam\Multitenant\IsTenantModel;
...

class ExampleModel extends Model
{
    use IsTenantModel;
...

You can specify migrations that are run only on tenant databases by placing them in the database/migrations/tenant directory. Migrations can be run on all tenants with php artisan multitenant:migrate and rolled back with php artisan multitenant:migrate:rollback. These commands will read the tenants table and automatically discover the relevant tenant databases. Each tenant maintains its own migration table.

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固