Show 404 if tenant doesn't exists

Hello everyone,

I am using subdomains for multi-tenancy in my Filamentphp v3 project.
The login pages for subdomains are like company-1.domain.com/dashboard/login
company-2.domain.com/dashboard/login

both company-1 and company-2 are registered as a tenant and the login page works good, but when I try to access company-3 subdomain which isn't registered, I still see the login page.
Is there a way to show 404 page if the tenant status is not active or it doesn't exist.

Thank you so much.
Solution
public function handle(Request $request, Closure $next): Response
    {
        if (!Auth::check()) {
            $slug = explode('.', $request->getHost())[0]; // Extract the subdomain

            // Check if the tenant exists
            if (!Company::where('slug', $slug)->exists()) {
                throw new NotFoundHttpException(); // Show 404 page if not found
            }

            return $next($request);
        }

        return $next($request);
    }
Was this page helpful?