简体   繁体   中英

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+. I have a companies table, and all other tables have a company_id column.

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...

How do I do that?

Is the parameter $tenantColumn equivalent to the company_id column of each of the tables?

Is the parameter $tenantId refers to the id of each company contained in the column company_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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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