Advice: How to access "related" models in a different database

I am using stancl/tenancy to provide a multi-tenant application with multi-database tenancy. I have two panels, one for Admins, and one for Tenants. From the Admin panel I have access to a Tenant Resource, however, I'd like to then be able to access the "related" models from that Tenant's database: This can be achieved by running a query within the Tenant's context, with either
$tenant = // The current Tenant Model

// and then
tenancy()->initialize($tenant);
User::query();

// or:
// @var $user ?User
$users = null
$tenant->run(function () use ($users) {
$users = User::query();
});
$tenant = // The current Tenant Model

// and then
tenancy()->initialize($tenant);
User::query();

// or:
// @var $user ?User
$users = null
$tenant->run(function () use ($users) {
$users = User::query();
});
i'm looking for some advice on how best to achieve this, "The Filament Way". Do I: - Create a Page, within the TenantResource /tenant/{id}/user. Then use "custom" infolists on these pages? - Is there a way to create a "faux" RelationshipManger? - another way I'm ignorant to? thanks, and sorry for the "advice" question, I'm fairly new to Filament world and just wanna make sure I'm not missing anything along the journey.
4 Replies
Matthew
Matthew2w ago
Multi database tenancy is very tricky. I'd consider whether that is really what you need, these days they are falling out of fashion. Or, assuming your using a proper database engine, if you are using UUID, what about a single database that collects the views from the others into single virtual tables and build the relationships to there. Alternatively, can you not just tenant the Admin user?
Illizian
IllizianOP2w ago
We have "good" reasons to opt for this architecture, and we have user/tenant impersonation. The UX would be vastly improved with the above approach though
awcodes
awcodes2w ago
I think you’re loosing the scope of what a tenant is in a multi db setup. The entire point of multi db is that they can’t access each other. It’s a security safeguard.
Illizian
IllizianOP2w ago
I don't want them to access each other. I want to access them from the central app.

Did you find this page helpful?