I have secondary connection that comes dynamically and here is it's code

 'userdb' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => DYNAMIC_DATABASE_NAME,
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => false,
        ],

So, once use logged in I want to update update this database dynamically and reconnect to this userdb.

I have used following code but this is not giving persistent DB connection I have to this this everytime manually. Any suggestions ?

DB::purge('userdb');
config(['database.connections.userdb.database' => 'test']);
DB::reconnect('userdb');
DB::connection('userdb');

1 Answers

0
Jignesh Joisar On

1.first set database name

\Config::set('database.connections.userdb.database','test');

2.after set database name you need to purge the DB cache

\DB::purge('userdb');

3.set default connection after u purge db cache

\DB::setDefaultConnection('userdb');