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

jackmu95 picture jackmu95  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

felixsanz picture felixsanz  路  3Comments

kerbylav picture kerbylav  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments