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

lawre17's avatar

Multitenancy with diffrent databases and spatie multitenancy package

I have worked on a project before and implemented multitenancy but it was pure Laravel 10, I am now trying the same with filament but I have issues I can't figure out:

  1. I used cookies to set the current tenant on my old project. The user logs in and Laravel triggers an event which I listen to and set the cookie. Spatie plugin has a way of setting the tenant for every request using a tenant finder contract and that's how that project works. Now I have noted filament does not use Laravel's authentication for the event is not being triggered.
  2. I tried overriding filament auth by a custom auth class that extends that of Filament but the Spatie Tenant finder is being triggered way before the user is authenticated and docs say if you put middleware in the authMiddleware array it will apply to all authenticated routes.

Has anyone worked on this before?

0 likes
4 replies
lawre17's avatar

I am not Identifying tenants with Domains I just want one domain and tenant to be identified on login

m7vm7v's avatar

Hi @lawre17, you can try opening the multitenancy.php. Inside the "switch_tenant_tasks" array add a new task, where you can add the necessary functionality.

<?php

namespace App\Support\SwitchTenantTasks;

use Carbon\Carbon;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;

class UpdateAuthenticationCookieTask implements SwitchTenantTask
{
    public function makeCurrent(Tenant $tenant): void
    {
         ....
    }

    public function forgetCurrent(): void
    {
        ....
    }
}
lawre17's avatar

Hello @m7vm7v Thank you for the reply but the problem Is not Switching tenants the issue is filamentphp that I am using sending an ajax request before setting the cookie which is used to identify tenant.

The class above extents filament Auth class and I override the method to dent the cookie once the user is logged in.

This class is custom also it extends the spatie tenant fimder as per the docs. Now my issue is the request is supposed to run for authenticated users where the cookie will already be set. but it runs when filamentmakes an ajax request and as per the documentation the middleware is supposed to be on authGuardMidlleware.

use Filament\Panel;
 
public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->authMiddleware([
            // ...
        ]);
}

Please or to participate in this conversation.