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

robjbrain picture robjbrain  路  64Comments

PheRum picture PheRum  路  112Comments

JosephSilber picture JosephSilber  路  176Comments

rafaelrenanpacheco picture rafaelrenanpacheco  路  64Comments

mrahmadt picture mrahmadt  路  61Comments