承接 solutosoft/laravel-multitenant 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

solutosoft/laravel-multitenant

Composer 安装命令:

composer require solutosoft/laravel-multitenant

包简介

The multi tenant control with shared table

README 文档

README

Build Status Total Downloads Latest Stable Version

The Shared Database used by all tenants, means that we keep data from all the tenants in the same database. To isolate tenant specific data, we will have to add a discriminator column like tenant_id to every table which is tenant specific, and to make sure that all the queries and commands will filter the data based on it.

With this strategy dealing with tenant shared data is simple, we just don't filter it. Isolating data is what we need to deal with. For this we need to make sure that ALL the queries and the commands that deal with tenant specific data get filtered by the tenant_id.

This extension allows control the Eloquent with shared database used by all tenants.

Installation

The preferred way to install this library is through composer.

Either run

composer require --prefer-dist solutosoft/laravel-multitenant "*"

or add

"solutosoft/laravel-multitenant": "*"

to the require section of your composer.json.

Usage

  1. Create table with tenant_id column:
/**
 * Run the migrations.
 *
 * @return void
  */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('firstName');
        $table->string('lastName');
        $table->string('login')->nullable();
        $table->string('password')->nullable();
        $table->string('remember_token')->nullable();
        $table->string('active');
        $table->integer('tenant_id')->nullable(true);
    });

    $Schema::create('pets', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('tenant_id');
    });
}
  1. Uses the MultiTenant trait, it add a Global Scope filtering any query by tenant_id column.
<?php

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Solutosoft\MultiTenant\MultiTenant;
use Solutosoft\MultiTenant\Tenant;

class User extends Model implements Tenant
{
    use MultiTenant, Authenticatable;

    /**
     * @inheritdoc
     */
    public function getTenantId()
    {
        return $this->tenant_id;
    }

    ...
}

class Pet extends Model
{

  use MultiTenant;

  ...

}

Now when you save or execute same query the tenant_id column will be used. Example:

// It's necessary will be logged in

$users = App\User::where('active', 1)->get();
// select * from `users` where `active` = 1 and tenant_id = 1

$pet = Pet::create(['name' => 'Bob']);
// insert into `pet` (`name`, 'tenant_id') values ('Bob', 1)

Auth Service Provider

It's necessary change the authentication service provider:

  1. Creates new file: app/Providers/TenantUserProvider.php
<?php

namespace App\Providers;

use Illuminate\Auth\EloquentUserProvider;
use Solutosoft\MultiTenant\TenantScope;

class TenantUserProvider extends EloquentUserProvider
{

    protected function newModelQuery($model = null)
    {
        return parent::newModelQuery($model)->withoutGlobalScope(TenantScope::class);
    }

}
  1. Edit app/Providers/AuthServiceProvider.php
<?php

namespace App\Providers;

class AuthServiceProvider extends ServiceProvider
{
    ...

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        ...

        Auth::provider('multitenant', function($app, $config) {
            return new TenantUserProvider($app['hash'], $config['model']);
        });
    }
}
  1. Edit config/auth.php
return [
    ....
    'providers' => [
        'users' => [
            'driver' => 'multitenant',
            'model' => App\Models\User::class,
        ],
    ],

    ...

统计信息

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

GitHub 信息

  • Stars: 25
  • Watchers: 2
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-12-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固