0

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');
4

1 Answer 1

0

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');
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting this error "Class 'App\Http\Controllers\Auth\Config' not found"
I already know this solution and I also written in my question Please check my question. The issue is when I change my page connection got reverted to old one.

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.