nate.a.johnson

I use Landlord and it's really and simple. My advice, if you know your app is going to be multi tenant then build it in at the beginning. It's a lot easier than trying to go back and figure out how to add it later. When I first started, I built out my app and added two tenants so that I could test that my data and logins were complete segregated. My multi tenancy is based off of the url used to enter the application. It uses a single database and separates data with a tenant_id column in the tables where that makes sense (users, companies, etc).

Here's a middleware that ensures every request in my app has multi tenancy applied correctly.

<?php

namespace App\Http\Middleware;

use App\Tenant;

use Closure;
use Exception;
use Landlord;

class TenantSelector
{
    public function handle($request, Closure $next, $guard = null)
    {
        $domain = $request->root();

        $tenant = Tenant::where('domain', $domain)->first();

        if ( ! $tenant) {
            throw new Exception('Unknown Tenant');
        }

        Landlord::addTenant('tenant_id', $tenant->id);

        return $next($request);
    }
}

Return to Thread...