laravel中动态连接多个数据库
问题描述
我正在构建一个需要连接 2 个数据库的应用程序.第一个是静态的,另一个是动态的.config/database.php 就像
I'm building an application which requires connecting 2 database. first one is static and another one is dynamic. config/database.php is like
和型号代码是一样的
如果我提供静态连接详细信息,我可以连接多个数据库,但如果我提供动态连接详细信息,我将无法连接数据库
I am able to connect multiple databases if I give static connection details but I am unable to connect database if I give dynamic connection details like
或
在不影响应用程序性能的情况下动态连接多个数据库的最佳方法是什么?
What is the best way to connect multiple databases dynamically without effecting performance of application?
推荐答案
在运行时更改连接的一种方法是通过配置设置值:
One way of changing the connection at runtime is to set the values via the config:
这可以应用在中间件中,例如在租户数据库之间动态切换.
This can be applied in a middleware to dynamically switch between tenant databases, for example.
您还可以通过 DB 外观指定连接:
You can also specify a connection via the DB facade:
这篇关于laravel中动态连接多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!