Framework: REQUEST: Specifying DB connection information dynamically instead of config key

Created on 11 Jul 2013  路  4Comments  路  Source: laravel/framework

Currently there is not way to create dynamic connections on Laravel other than specifying a config key and using

`DB::connection('key')

It would be good to specify the connection information dynamically, something like this

DB::createConnection(array(
              'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'dbname',
            'username'  => 'dbuser',
            'password'  => 'dbpass',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            )

Most helpful comment

Config::set('database.connections.key', array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'dbname',
    'username'  => 'dbuser',
    'password'  => 'dbpass',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
));

With that DB::connection('key'); would just work.

All 4 comments

Config::set('database.connections.key', array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'dbname',
    'username'  => 'dbuser',
    'password'  => 'dbpass',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
));

With that DB::connection('key'); would just work.

crynobone, thanks for this. Does it require specify any config info named key on database.php? or will it just create this config on the fly?

You can create the config on the fly, it would just be appended to "database.connections" config.

Thanks, that works. Appreciate your help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CupOfTea696 picture CupOfTea696  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

felixsanz picture felixsanz  路  3Comments

ghost picture ghost  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments