Search code examples
laravelsubdomainmulti-tenant

Abort if tenant not found - Laravel


I am using tenancyforlaravel package and all are working fine. When I hit the tenant does not exist in my app then I get an error

Tenant could not be identified on domain app.localhost

how can show an abort message here?


Solution

  • There's a better approach the package provides.

    In TenancyServiceProvider add this in the boot method:

    If you are using TenancyByDomain Middleware, use $onFail property, or you may change the Middleware class if you are not using InitializeTenancyByDomain to the targeted middleware.

    \Stancl\Tenancy\Middleware\InitializeTenancyByDomain::$onFail = function ($exception, $request, $next) {
        return redirect('https://my-central-domain.com/');
    };
    

    Head to this docs page for more info: https://tenancyforlaravel.com/docs/v3/tenant-identification/#subdomain-identification