0

I have a series of databases named server_1 to server_100. Based on the URL parameter that the users select, I would like to connect to that database. Is there a way to accomplishing this without defining 100 separate connections in .env?

4
  • Exposing actual db name in frontend is a major security vulnerability. Commented Oct 26, 2021 at 5:34
  • You can use tenancy concept in laravel tenancy.dev Commented Oct 26, 2021 at 5:36
  • It must help you stackoverflow.com/questions/36085131/… Commented Oct 26, 2021 at 5:36
  • Or you can define all the db connection into databases. And based on url changes. By using middleware you can set the connection dynamically Commented Oct 26, 2021 at 5:36

1 Answer 1

1

You have to change database connection on fly and also dont forget to purge connection.

Config::set("database.connections.mysql.host", "127.0.0.1");
Config::set("database.connections.mysql.database", "database_name");
Config::set("database.connections.mysql.username", "username");
Config::set("database.connections.mysql.password", "password");
DB::purge('mysql');
Sign up to request clarification or add additional context in comments.

2 Comments

Are the configurations saved until script execution is complete, or is it saved until the DB is purged again?
It must require to purge connection, when ever you change.

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.