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.

1 Answer 1

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

]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.