F
Filamentβ€’15mo ago
skyrowl

How to change database connection of filament tables and forms

Hey, I'm wondering if there is any way to change the database connection ? Basically default connection is X and on a page I want to access connection Y that is another DB to display datas that are stored there only, I have the connection but don't really know how to pass it to filament tables/forms
30 Replies
Dennis Koch
Dennis Kochβ€’15mo ago
The connection should be set on the model then.
LeandroFerreira
LeandroFerreiraβ€’15mo ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation β€” freeing you to create without sweating the small things.
skyrowl
skyrowlβ€’15mo ago
thx will try that πŸ˜„ I know it's not really about filament I'm sorry but would you know if it's possible to pass Illuminate\Database\MySqlConnection to eloquent models from functions ? Connections are dynamic and not hard configured I'd like to do something like Model::setConnection($mySqlConnection)->getQuery()
Dennis Koch
Dennis Kochβ€’15mo ago
Seems like Eloquent provides an on() method Model::on('your-connection')
skyrowl
skyrowlβ€’15mo ago
I tried to look at that sadly it's only for configured databases, it only accepts a string parameter, not a connection πŸ˜” last resort would be to use query builder but I'm not sure how filament will behave with that and mainly was looking for a more "Laravel" way
Dennis Koch
Dennis Kochβ€’15mo ago
What scenario do you have where you need to configure a custom connection per model?
skyrowl
skyrowlβ€’15mo ago
I'm using tenancy (stancl's one) And I'm willing to give access to tenant datas from the central domain
Dennis Koch
Dennis Kochβ€’15mo ago
I'm using tenancy (stancl's one)
I thought so.
And I'm willing to give access to tenant datas from the central domain
What does that mean? Impersonation? Tenant switching?
skyrowl
skyrowlβ€’15mo ago
Like they access, configure and add content from the central domain panel, something like central.domain/{tenant}/products
Dennis Koch
Dennis Kochβ€’15mo ago
So it's just a simple tenant system? Did you check the docs? https://filamentphp.com/docs/2.x/admin/resources/getting-started#stancltenancy
Filament
Getting started - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
skyrowl
skyrowlβ€’15mo ago
Yep I did and it does not fit my needs that's why I'm only using forms and tables
Dennis Koch
Dennis Kochβ€’15mo ago
Ohh. I didn't notice you aren't using the admin panel.
skyrowl
skyrowlβ€’15mo ago
I want content to be managed from the central domain thus I don't want to initialize tenancy, only pass the right connection to the tables/forms Yes that's a bit of a challenge that way haha
Dennis Koch
Dennis Kochβ€’15mo ago
But that's just tenancy based on path instead of domain in that case? And totally unrelated to Filament?
skyrowl
skyrowlβ€’15mo ago
Not really cause if I want to initialize tenancy I will lose sessions and global infos Yep my main question was if it is possible/there is a built in function to use a different database in forms and tables πŸ˜…
Dennis Koch
Dennis Kochβ€’15mo ago
Okay. Got your issue now. Not sure what's the best way though. And I still think this is an Eloquent question πŸ˜…
skyrowl
skyrowlβ€’15mo ago
Asked on Laravel discord already for that, waiting for an answer :/
Dennis Koch
Dennis Kochβ€’15mo ago
You could configure a connection via Config::set() and use it inside ->on()
skyrowl
skyrowlβ€’15mo ago
I'm not sure it is possible with stancl as dbs are created at the same time as tenants
Dennis Koch
Dennis Kochβ€’15mo ago
How do you currently get your connection?+
skyrowl
skyrowlβ€’15mo ago
tenancy()->getTenant('id')->getConnection() Not 100% sure but that's something like that, I'll be back on my comp in some minutes
Dennis Koch
Dennis Kochβ€’15mo ago
But that requires Stancl Tenancy to be active via a Middleware or similar? How does that work with "Not really cause if I want to initialize tenancy I will lose sessions and global infos"
skyrowl
skyrowlβ€’15mo ago
Not at all, you get the tenant object and through that you can get the MySqlConnection object
Dennis Koch
Dennis Kochβ€’15mo ago
Yes. But the tenant already needs to be identified, right? Where does that happen? As tenancy()->getTenant() gives you the current tenant
skyrowl
skyrowlβ€’15mo ago
I don't really know how to describe it but you can access tenant objects without being in tenancy and above is the same as Tenant::find('id')->getConnection() It's just using their helper functions but in facts that is a really basic query
Dennis Koch
Dennis Kochβ€’15mo ago
Okay, find() is something else then getCurrent()
skyrowl
skyrowlβ€’15mo ago
Nah it's not getCurrent it's getTenant($id) there is also findBy and other functions Welp I red again the doc and might try to use another function and will see if it does the same
Dennis Koch
Dennis Kochβ€’15mo ago
Shouldn't $connection->getName() give you the connection name?
Dennis Koch
Dennis Kochβ€’15mo ago
If that doesn't work, I think your best try is to write to the config as I mentioned before. See this thread on Laracasts: https://laracasts.com/discuss/channels/tips/set-up-dynamic-database-connection-globally?page=1&replyId=61963
Laracasts
skyrowl
skyrowlβ€’15mo ago
yep you're right I was doing it wrong I'm not getting the right database πŸ˜… will take a look at that thx a lot! thx a lot I did exactly what I was searching for with cached config πŸ˜„ now I'll need to setup something that is more global πŸ€”