Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JoeSWDataNet's avatar

Stancl Tenancy not switching to tenant database

When connecting to my site using foo.localhost, the database being used is supposed to switch and yet it tried to search through the primary database. I started with jetstream and am using the verification system.

If I use a route such as Route::get('/', function () {return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');})->name('dashboard');, this works and is recognised. But anything from fortify used to log in grabs from the wrong database.

I have tried editing the configs, adding the middleware as seen here 'middleware' => ['web', PreventAccessFromCentralDomains::class, InitializeTenancyByDomain::class, ], but it seems to have no effect. Here is a snippet of the routes I am using within tenant.php

0 likes
1 reply
LaryAI's avatar
Level 58

It looks like you're having trouble with Stancl Tenancy not switching to the tenant database when using Fortify for authentication. This is a common issue when dealing with multi-tenancy and authentication systems. Here are a few steps to ensure that the tenant database is correctly switched:

  1. Ensure Middleware Order: The order of middleware is crucial. Make sure that the tenancy initialization middleware (InitializeTenancyByDomain) is executed before any other middleware that might interact with the database.

  2. Check Configuration: Ensure that your config/database.php is correctly set up to handle tenant connections. The tenancy package should be configured to switch databases based on the tenant.

  3. Custom Middleware: Sometimes, you might need to create custom middleware to ensure that the tenant context is correctly set before any Fortify actions are taken.

Here's a step-by-step solution:

Step 1: Ensure Middleware Order

Make sure your tenant.php routes file has the correct middleware order:

Step 2: Check Database Configuration

Ensure your config/database.php is set up to handle tenant connections. You should have something like this:

'connections' => [
    'tenant' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => null, // This will be set dynamically
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
],

Step 3: Custom Middleware (if needed)

If the above steps do not resolve the issue, you might need to create custom middleware to ensure the tenant context is set before Fortify actions. Here’s an example of custom middleware:

namespace App\Http\Middleware;

use Closure;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;

class EnsureTenantDatabase
{
    public function handle($request, Closure $next)
    {
        // Initialize tenancy
        app(InitializeTenancyByDomain::class)->handle($request, function () {});

        return $next($request);
    }
}

Register this middleware in your app/Http/Kernel.php:

protected $routeMiddleware = [
    // Other middleware
    'ensure.tenant.database' => \App\Http\Middleware\EnsureTenantDatabase::class,
];

Then, use this middleware in your routes:

Route::middleware([
    'web',
    'ensure.tenant.database',
])->group(function () {
    // Your routes here
});

By following these steps, you should be able to ensure that the tenant database is correctly switched when using Fortify for authentication.

Please or to participate in this conversation.