Laravel 6 config()->get('database.connections.mysql') not matching DB:connection()
Asked Answered
N

2

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?
Nitrogen answered 3/1, 2020 at 15:13 Comment(13)
Is the missing quote ' a real TYPO or something you added/missed out when creating the questionConant
missed upon creating the question, fixed it now. Thanks.Nitrogen
Take a look at https://mcmap.net/q/1780877/-laravel-change-database-connection-for-a-specific-url - maybe it will helpRehash
Changing config won't work since db singleton probably already exists.Rumple
Isn't there a cache you need to clear...Greenstein
Thanks @MarcinNabiałek I am pulling the tenants from a database table and would like to avoid hard coding them. Otherwise I could use that way though.Nitrogen
Thanks @EliasSoares - do you know how to perform the way Marcin posted dynamically - without writing the tenants to a file?Nitrogen
@Nitrogen What I mean you should try this: DB::disconnect(); Config::set('database.mysql.database', 'tenant_foo'); DB::reconnect();Rehash
Thanks @AlexBarker - as posted I think all caches are clear - also in the browser. Any caching I might have missed?Nitrogen
@MarcinNabiałek that did it! - I (undocumented) tried the disconnect before, but did not reconnect. - Thank you so much. Would you mind adding this as an answer so I can set it as solution?Nitrogen
@Nitrogen Done. You're welcome :)Rehash
@Nitrogen php artisan config:cache cache:clear does do this one.Greenstein
Thanks @AlexBarker - I thought I had noted it correctly. Sorry for the confusion. Updated it accordingly.Nitrogen
W
2

To dynamically change database name you should use:

DB::disconnect(); 
Config::set('database.mysql.database', 'tenant_foo'); 
DB::reconnect();
Wintery answered 3/1, 2020 at 21:0 Comment(5)
not working in my case! My laravel version is 5.6. Please help me !Subarctic
No longer working, see Malki Mohamed's answer .connections. needs to be added now with the new config structureHedonism
@Hedonism 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 100Rehash
@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();Hedonism
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 oneHedonism
D
2

This worked for me:

\DB::disconnect('mysql'); 

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

\DB::reconnect('mysql');
Dezhnev answered 31/5, 2022 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.