0

I am developing a multi database with common login laravel apps where user database is identified from a common table in default database.

After login it alters the default database setting to user defined database.

But it just goes to default database again in the next page request.

I alter the database as follows:

$config = \Config::get('database.connections.mysql');
            $config['database'] = $check_db[0]->database_name;
            config()->set('database.connections.mysql', $config);
            DB::purge('mysql');
            DB::reconnect('mysql');

Is there any other way to make it persistent through out the user session ?

Enayet Hossain
  • 187
  • 1
  • 4
  • 14
  • Do u already check this https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel? – Sok Chanty Oct 10 '20 at 14:48
  • yes... but none of the links works for me... the database is always reset to default in next request( not persistent) – Enayet Hossain Oct 10 '20 at 14:57
  • if i echo the current database connection from any page it shows the right database(dynamically set) connection... but the logged in user info shows from master database which is very confusing – Enayet Hossain Oct 10 '20 at 15:20
  • Do you use Eloquent? Can you show me a source code of User model (or the model you are authenticating agaisnt) – Jakub Truneček Oct 10 '20 at 18:42
  • actually, it can connect to the required DB during login...but after that if i run Auth::user()->id it will show the user id from the master/default database not from the connected database. My concern is: can we remove the master/default database connection and reconnect with tenant database for the whole user session? – Enayet Hossain Oct 11 '20 at 04:25

0 Answers0