简体   繁体   English

如何在Laravel 5.2中使用HipsterJazzbo / Landlord包 - 单个数据库多租户?

[英]How can I use HipsterJazzbo/Landlord package -single database multi-tenancy- in Laravel 5.2?

I am using https://github.com/HipsterJazzbo/Landlord single database multi-tenancy solution for Laravel 5.2+. 我正在为Laravel 5.2+使用https://github.com/HipsterJazzbo/Landlord单一数据库多租户解决方案。 I have a companies table, and all other tables have a company_id column. 我有一个公司表,所有其他表都有一个company_id列。

I'm not sure how to implement the call Landlord::addTenant($tenantColumn, $tenantId) in a global Middleware, in an auth system or in the constructor of a base controller... I'm confused... 我不确定如何在全局中间件,auth系统或基本控制器的构造函数中实现调用Landlord :: addTenant($ tenantColumn,$ tenantId) ......我很困惑......

How do I do that? 我怎么做?

Is the parameter $tenantColumn equivalent to the company_id column of each of the tables? 参数$ tenantColumn是否等效于每个表的company_id列?

Is the parameter $tenantId refers to the id of each company contained in the column company_id? 参数$ tenantId是否指的是company_id列中包含的每个公司的ID?

thank you! 谢谢!

A global middleware is not the good place because you don't have access to the authenticated user. 全局中间件不是好地方,因为您无权访问经过身份验证的用户。 A solution is to create a route middleware, for example: 解决方案是创建路由中间件,例如:

<?php

namespace App\Http\Middleware;

use Closure;
use HipsterJazzbo\Landlord\Facades\Landlord;
use Illuminate\Support\Facades\Auth;

class LimitToCurrentCompany
{
    public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            $tenant = Auth::user()->currentCompany;
            Landlord::addTenant($tenant);
        }

        return $next($request);
    }
}

add it to $routeMiddleware array in app/Http/Kernel.php: 将它添加到app / Http / Kernel.php中的$ routeMiddleware数组:

protected $routeMiddleware = [
    […]
    'limitToCurrentCompany' => \App\Http\Middleware\LimitToCurrentCompany::class,
];

Then in your routes file: 然后在你的路线文件中:

Route::group(['middleware' => 'limitToCurrentCompany'], function () {
    // your routes
});

And yes, like said in the comment, $tenantColumn is the company_id and $tenantId is the id of the company. 是的,就像评论中所说的那样,$ tenantColumn是company_id,而$ tenantId是公司的id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM