0

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.

user2094178
  • 9,204
  • 10
  • 41
  • 70
Jacob Lambert
  • 7,449
  • 8
  • 27
  • 47

1 Answers1

0

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,
    ]

]
user2094178
  • 9,204
  • 10
  • 41
  • 70