Skip to content

Instantly share code, notes, and snippets.

@HelgeSverre
Created June 13, 2017 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HelgeSverre/718cf59a2793ffa91c0e08afee3c8f2d to your computer and use it in GitHub Desktop.
Save HelgeSverre/718cf59a2793ffa91c0e08afee3c8f2d to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use App\User;
use Auth;
use Closure;
use HipsterJazzbo\Landlord\Facades\Landlord;
class TenantScope
{
public function handle($request, Closure $next)
{
// If user is logged in, fetch associated tenant model
if (Auth::check()) {
$tenant = Auth::user()->tenant;
if ($tenant != null) {
// This will cause all models to be scoped to that tenant
Landlord::addTenant('tenant_id', $tenant->id);
}
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment