hipsterjazzbo / Landlord

A simple, single database multi-tenancy solution for Laravel 5.2+
MIT License
612 stars 142 forks source link

Example Middleware for Laravel 5.5 #79

Open ServerJunge opened 7 years ago

ServerJunge commented 7 years ago

Hi guys,

does anyone have an example for a working middleware for Laravel 5.5 where you set the tenant by the users tenant id and with auth:api routes?

Route::middleware('auth:api:web')

darrencoutts118 commented 7 years ago

https://github.com/samtheson/voyagerlandlord/blob/master/app/Http/Middleware/Landlorder.php

hmamman commented 6 years ago
<?php

namespace App\Http\Middleware;

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

class Tenancy
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = Auth::guard()->user();

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

        return $next($request);
    }
}