Skip to content
Advertisement

Changing MySQL database to connect to based on URL parameter in Laravel 8

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?

Advertisement

Answer

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');
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement