I am currently handling a "multi db on the fly swap connections" sort of project.
So what I end up doing is the following:
$connectionName = uniqid();
\Config::set('database.connections.' . $connectionName, [/** db options **/]);
\Artisan::call('migrate', ['--database' => $connectionName]);
or
$connectionName = uniqid();
\Config::set('database.connections.' . $connectionName,[/** db options **/]);
$user = new User();
$user->setConnection($connectionName);
$user->first_name = 'Daisy';
$user->last_name = 'Demo';
$user->is_not_being_ignored_by_santa_this_year = 0;
$user->email = //and so so on
$user->save();
For the Artisan call I sort of understand why Laravel needs to refer to a connection in a string, saved in a config array.
However on the Eloquent Model itself I find it somehow cumbersome to have to write my DB connection into a config array. So it can be picked up by the "Singleton approach" \Config::get().. in the Model.
Is there something more elegant, where I can inject a configuration directly without having to write it into some super global ?
Or am I missing something ?
You would probably be better off creating a configuration array for each connection then you could switch between connections pretty easily by specifying which connection to use.
If you need to use multiple connections on the same model you can use the
on
method:so it would be something like
User::on('dbconnection2')->find(1)
If you just want to use different connections for different models, you can set the protected
$connection
property on the model:Hope that helps.
You could create a factory for your models and pass it the connection on bootstrapping your app:
Then in your code:
Something like this! Good luck! :-)
I build a multitenant laravel app and was surprised that there is no out-of-the-box way of doing that.
I have one app available via different subdomains and the subdomain should be the key to different configs, like the database connection.
You can easily adapt that to whatever criteria you need instead of the subdomain.
Via dynamically Config::set()
So my first attempt was to always use the "default" connection and to create a middleware that dynamically calls
Config::set("database.connection.default.host", "1.3.5.7");
and so on for all the other settings.But there are some downsides. For example it was pretty slow because I read all the values from the database and later on from redis. But the much bigger problem was to set the connection to the redis cache for example, because the redis connection is already established before the middleware it called to override the config setting.
Via own config files
So my second and current approach is to make it all via config files. So I created the following file structure:
Note: The underscore is important because the files are read in alphabetic order and our new file has to be read at first to use it in other config files.
The goal is to use
config('_myapp.DB_HOST')
to retrieve the DB_HOST value from config/[subdomain]/_myapp.php and use that as value in config/database.php. So config/_myapp.php only returns the content of _myapp.php for the specific subdomain.in config/_myapp.php:
and in config/_myapp.php:
Use this for example in config/database.php or whereever you need domain specific config:
Please ask if something doesn't work, took me quite some time to figure that stuff out.
Its all depends on how you handling your multiple connections.I have worked on similar requirement project.
Tenant Setup after login
.DatabaseConnection
.TenantContextSession
Tenant Model Abstract Class
Tenant Setup after login
DatabaseConnection
*TenantContextSession *
I hope this will help to work with your scenario, I have tried to mention all of aspect we had to covered, but if still there is something confusing and need explanation from my side please let me know.