Laravel中的动态数据库更改 [英] Dynamic Database change in Laravel
问题描述
我正在使用 DB :: disconnect
和 DB :: reconnection
来连接多租户系统中的数据库Laravel 5.2。
I'm using DB::disconnect
and DB::reconnection
for connect database for multi tenant system in Laravel 5.2.
代码如下:
DB::disconnect();
Config::set('database.connections.mysql', $config);
DB::reconnect();
其中$ config是要设置的一组配置,当我必须更改时,这可以正常工作唯一的数据库名称,但是当我想更改数据库的前缀时,它每次都使用旧数据库时都无法使用。
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.
我尝试了 config:cache
来自中间件,我在其中更改了连接,但它删除了所有输入变量。
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?
推荐答案
您需要清除数据库对象的缓存:
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();
这篇关于Laravel中的动态数据库更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!