1

I'm trying to connect to db dynamically through:

DB::disconnect('mysql');
Config::set('database.connections.mysql.database', 'db_1');

When I use DB::table('table_from_db_1')->..., it works perfectly. I get data from db_1;

However, when I try to get DB::connection()->getDatabaseName(), it still returns the original db name and NOT the db_1.

Question:

  1. Why is this happening?
  2. How do I efficiently test if I have actually connected to the dynamic database?
ACD
  • 1,431
  • 1
  • 8
  • 24

1 Answers1

0

For anyone having the same problem, solved the issue by using purge instead of disconnect.

DB::purge('mysql'); // purge instead of disconnect
Config::set('database.connections.mysql.database', 'db_1');

Finally, use DB::connection()->getPdo(); inside try-catch to check if connection is valid.

ACD
  • 1,431
  • 1
  • 8
  • 24