Hi Dagord,
The way I recently tackled this issue was by having a database field databases
with the credentials for each respective entity. You can link the sub-domain to a data-field (for example, a column called "subdomain" with value "two") so you can easily retrieve the database credentials for the active sub-domain / entity. Once you've found a way to retrieve the database credentials for the active sub-domain, you can switch to that database using this bit of code:
config(['database.connections.tenant' =>
[
'driver' => 'mysql',
'host' => $tenant->host,
'username' => $tenant->username,
'password' => $tenant->password,
'database' => $tenant->database
]
]);
After you've done that you can use that connection in your eloquent builds. For example:
User::connection('tenant')->find($id);
I'm not sure whether or not this is the best best possible way to do this, however it appears to be one of the only ways to dynamically create connections (on the fly) in Laravel. Hope this helps!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community