Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the Database Connection Dynamically in Laravel [duplicate]

I have the master database with login table and corresponding database settings for each user. On login I should dynamically change the db settings fetching from the table. I can change the db connection but this is not persisting.

Config::set("database.connections.mysql", [
'driver' => 'mysql',
"host" => $usr_host,
"database" => $usr_database,
"username" => $usr_username,
"password" => $usr_password,
...
]);

edit: New database is created for each user when he/she registers with the app and thus i dont have the database connection for each user defined in the config/database.php

like image 562
tyro Avatar asked Oct 16 '17 16:10

tyro


1 Answers

This way you can set new parameter when it comes to database:

 \Config::set('database.connections.mysql.database', $schemaName);

Remember about PURGE to persist this settings

 DB::purge('mysql');

Cheers!

like image 150
Adam Kozlowski Avatar answered Sep 16 '22 14:09

Adam Kozlowski