3

Prerequisites

In my local environment I am working with multiple tenants and Redis (Auth required).
To serve the project I am using Valet.

For this case I am addressing these two connections:

- basic_foo (is defined in my .env)
- tenant_foo (is the one to change to during a request)

Until now I successfully changed the connections like so:

config()->set('database.connections.mysql', 
       array_merge(
        config()->get('database.connections.mysql') , 
        ['database' => 'tenant_foo']
    ); 

Problem

However, now I am seeing an issue with the query builder, keeping or falling back to the basic connection.

I get the expected connection results of tenant_foo (same for Redis) when I run

dd(config()->get('database.connections.mysql'));

I get the wrong but apparently active results of basic_foo when I run

dd(\DB::connection()); // returns Illuminate\Database\MySqlConnection

So all in all the app will return this Illuminate\Database\QueryException

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'basic_foo.table_bar' doesn't exist...

where it should search for

'tenant_foo.table_bar'

Things that did not solve the problem yet

  • restarting Redis
  • reinstalling Redis
  • php artisan config:cache
  • php artisan cache:clear
  • php artisan route:clear
  • php artisan view:clear
  • php artisan optimize
  • composer dump-autoload

Simply changing the database name to tenant_foo like below is not enough, as the config array remains the same of basic_foo.

\DB::connection()->setDatabaseName('tenant_foo');

Thoughts

  • I want to change the config-array the of \DB::connection(), but I don't know another way than the config->set().
  • I installed Telescope could this affect the db connection?
  • Any other ideas?
leopold
  • 1,971
  • 1
  • 19
  • 22

2 Answers2

2

To dynamically change database name you should use:

DB::disconnect(); 
Config::set('database.mysql.database', 'tenant_foo'); 
DB::reconnect();
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • not working in my case! My laravel version is 5.6. Please help me ! – Nirav Chavda Dec 31 '20 at 19:33
  • No longer working, see Malki Mohamed's answer `.connections.` needs to be added now with the new config structure – Brett Jun 17 '22 at 01:20
  • @Brett Not working in Laravel 6 or in Laravel 9? This question was asked for Laravel 6 so it's normal it might not work in Laravel 9, Laravel 20 or Laravel 100 – Marcin Nabiałek Jun 17 '22 at 07:09
  • @MarcinNabiałek just letting people know it no longer works for 8+ // Make sure you've disconnected first config()->set('database.connections.mysql.database', $account->username); $connection = DB::connection('mysql'); // Do stuff here $connection->disconnect(); – Brett Jun 20 '22 at 02:42
  • https://github.com/laravel/laravel/blob/6.x/config/database.php - shows that there is a connections array for laravel 6, so the Malki's answer actually still stands as the correct one – Brett Jun 20 '22 at 02:45
2

This worked for me:

\DB::disconnect('mysql'); 

Config::set('database.connections.mysql.database', 'tenant_foo');

\DB::reconnect('mysql');
Malki Mohamed
  • 1,578
  • 2
  • 23
  • 40