Home > front end >  Changing MySQL database to connect to based on URL parameter in Laravel 8
Changing MySQL database to connect to based on URL parameter in Laravel 8

Time:10-26

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?

CodePudding user response:

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');
  • Related