I know that in Laravel you can use multiple database connections by specifying them in the config/database.php
file, then using DB::connection('my_conn_name')
, but is there anyway to use a connection that is not specified in the config/database.php
file?
I am writing an archiving application, so the user can specify what connection they would like to use for the process (host, user and password), and I am hoping that I can return the results from show databases
for the supplied connection.
After the user has specified the db parameters you could store it in a session to populate a custom connection at config/database.php
:
'connections' => [
'mysql' => [
'...'
],
'testing' => [
'...'
],
'custom' => [
'driver' => 'mysql',
'host' => session()->get()->db_host,
'database' => session()->get()->db_database,
'username' => session()->get()->db_username,
'password' => session()->get()->db_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
]