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?
6
  • Have you tried to create a second Dataset in configuration. Check this complete example : link Commented Jun 24, 2019 at 14:07
  • Not an option, I will be having 500+ dynamic databases and no code should be touched when it starts rolling. Commented Jun 24, 2019 at 14:21
  • DId you try something like this link Commented Jun 24, 2019 at 14:31
  • did you write DB::reconnect() ? Commented Jun 24, 2019 at 14:58
  • @FrédéricKlee can't do it like that since I have to change the connection entirely throughout the request lifecycle. Commented Jun 25, 2019 at 4:36

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.