1

I'm using DB::disconnect and DB::reconnection for connect database for multi tenant system in Laravel 5.2.

The code is as:

DB::disconnect();
Config::set('database.connections.mysql', $config);
DB::reconnect();

Where $config is an array of configurations to be set, this is working fine when I have to change the only database name, but when I want to change the prefix for the database it is not working, every time it takes old database.

I have tried to config:cache from the Middleware where I change the connection but it removes all input variables.

So, What Can I do to change prefix of DB connection? Any help?

1 Answer 1

3

You'll need to purge the cache of the database object:

// Will disconnect automatically
DB::purge('mysql');

// Register new config
Config::set('database.connections.mysql', $config);

// Will reconnect automatically
DB::table('table')->get();

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, is anything in laravel that prevent cache for database?
I don't think there is. "Databse cache" means "sql connection" — that means if you have no DB cache, laravel will connect and disconnect everytime a query is made (which is low performance and stressing proof)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.