Not sure if this will help
https://filamentphp.com/docs/3.x/panels/tenancy#applying-middleware-to-tenant-aware-routes
So you will have to make it persistent so the middleware check will run on each subsequent AJAX request.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm building a custom multi tenant setup with multiple databases (landlord plus one per tenant). I have created some middleware that checks for the existence of the tenant in the session and sets the database (either the correct tenant or the landlord if there is no tenant). This works correctly for all normal pages on the site.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Symfony\Component\HttpFoundation\Response;
class CheckTenant
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (Session::get('tenant')) {
Config::set('database.connections.mysql.database', 'tenant' . Session::get('tenant')->id);
DB::purge('mysql');
DB::reconnect('mysql');
} else {
Config::set('database.connections.mysql.database', 'landlord');
DB::purge('mysql');
DB::reconnect('mysql');
}
return $next($request);
}
}
However, when I open a modal, e.g. to create a new user, using the standard Filament CreateUser modal, the database instantly changes back to the default which is the landlord.
Can anyone point me in the right direction to either apply my database script to the modals or another way of setting the database?
I have looked the through the stancl/tenancy code and can't seem to work out how it actually switches the databases? Thanks in advance. Mike
Please or to participate in this conversation.