Laravel:动态连接数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36085131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: connect to databases dynamically
提问by Roboroads
I'm creating an application in Laravel 5(.1) where it is needed to connect to different databases. The only problem is that it's not known which databases it has to connect to, so making use of the database.php in config is not possible. A controller is in charge of making a connection with dynamically given connection details.
我正在 Laravel 5(.1) 中创建一个应用程序,需要连接到不同的数据库。唯一的问题是它不知道它必须连接到哪些数据库,因此在配置中使用 database.php 是不可能的。控制器负责与动态给定的连接细节建立连接。
How can I make a new connection to a database, including making use of the DB class? (Or is this possible)
如何与数据库建立新连接,包括使用 DB 类?(或者这可能)
Thanks in advance!
提前致谢!
回答by jszobody
The simplest solution is to set your database config at runtime. Laravel might expect these settings to be loaded from the config/database.php
file, but that doesn't mean you can't set or change them later on.
最简单的解决方案是在运行时设置数据库配置。Laravel 可能希望从config/database.php
文件中加载这些设置,但这并不意味着您以后不能设置或更改它们。
The config loaded from config/database.php
is stored as database
in Laravel config. Meaning, the connections
array inside config/database.php
is stored at database.connections
.
加载的配置config/database.php
存储database
在 Laravel 配置中。意思是,connections
里面的数组config/database.php
存储在database.connections
.
So you can easily override/change these connections like this:
因此,您可以像这样轻松地覆盖/更改这些连接:
Config::set("database.connections.mysql", [
"host" => "...",
"database" => "...",
"username" => "...",
"password" => "..."
]);
From there on out, any Eloquent models that use this mysql
connection will be using this new database connection config.
从那时起,任何使用此mysql
连接的Eloquent 模型都将使用此新的数据库连接配置。
I'd recommend doing this in a Service Provider if possible.
如果可能,我建议在服务提供商中执行此操作。
回答by feskr
I've stumbled upon the same problem.
我偶然发现了同样的问题。
You can actually change database settings in runtime and use them.
您实际上可以在运行时更改数据库设置并使用它们。
Use the config() function to set extra or overwrite existing connection settings.
使用 config() 函数来设置额外的或覆盖现有的连接设置。
config(['database.connections.mynewconnection' => {settings here}]);
Keep in mind that these settings are cached. So when you need to use the new settings, purge the DB cache for the connection you're gonna use.
请记住,这些设置已被缓存。因此,当您需要使用新设置时,请清除要使用的连接的数据库缓存。
DB::purge('mynewconnection');
You can also manipulate the default connection that is used. This can come in handy if you wish to use migrations over different connections and keep track of them with a migration table within the used connection. Or other cool stuff ofcourse...
您还可以操作使用的默认连接。如果您希望在不同的连接上使用迁移并使用所用连接中的迁移表跟踪它们,这会派上用场。或者其他很酷的东西当然......
DB::setDefaultConnection('mynewconnection');
回答by Derek
I ran into this problem too with a script that imports multiple MS Access DB files into MySQL and I was not satisfied with any of the solutions which suggested editing configuration at runtime. It was ugly and messy to dynamically create a new config for every single Access DB file that I wanted to import. After some playing, I manged to persuade Laravel to switch DBs on the existing connection like this:
我使用将多个 MS Access DB 文件导入 MySQL 的脚本也遇到了这个问题,我对任何建议在运行时编辑配置的解决方案都不满意。为我想要导入的每个 Access DB 文件动态创建一个新配置是丑陋和混乱的。玩了一段时间后,我设法说服 Laravel 在现有连接上切换数据库,如下所示:
$mysqlConn = DB::connection();
$mysqlConn->getPdo()->exec("USE $schemaName;");
$mysqlConn->setDatabaseName($schemaName);