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
The connection should be set on the model then.
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.
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()
Seems like Eloquent provides an
on()
method Model::on('your-connection')
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
What scenario do you have where you need to configure a custom connection per model?
I'm using tenancy (stancl's one)
And I'm willing to give access to tenant datas from the central domain
I'm using tenancy (stancl's one)I thought so.
And I'm willing to give access to tenant datas from the central domainWhat does that mean? Impersonation? Tenant switching?
Like they access, configure and add content from the central domain panel, something like
central.domain/{tenant}/products
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.
Yep I did and it does not fit my needs that's why I'm only using forms and tables
Ohh. I didn't notice you aren't using the admin panel.
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
But that's just tenancy based on path instead of domain in that case?
And totally unrelated to Filament?
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 π
Okay. Got your issue now. Not sure what's the best way though. And I still think this is an Eloquent question π
Asked on Laravel discord already for that, waiting for an answer :/
You could configure a connection via
Config::set()
and use it inside ->on()
I'm not sure it is possible with stancl as dbs are created at the same time as tenants
How do you currently get your connection?+
tenancy()->getTenant('id')->getConnection()
Not 100% sure but that's something like that, I'll be back on my comp in some minutesBut 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"
Not at all, you get the tenant object and through that you can get the MySqlConnection object
Yes. But the tenant already needs to be identified, right? Where does that happen?
As
tenancy()->getTenant()
gives you the current tenantI 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
Okay,
find()
is something else then getCurrent()
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
Shouldn't
$connection->getName()
give you the connection name?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
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 π€