Laravel同时动态连接到不同的数据库 [英] Laravel dynamic connections to different databases at the same time
问题描述
我正在构建一个Laravel库存管理应用程序,该应用程序允许每个子订户(用户)在单独的数据库中都有一个stock
.
I'm building a Laravel Stock Management Application that allows each subsriber (user) to have a stock
in a separate database.
所有用户的数据库都具有源自相同Laravel迁移的相同设计.
All the users' databases have the same design originating from the same Laravel migrations.
我有一个master_database
,其中包含一个stock
表,如下所示:
And I have a master_database
, which contains a stock
table as follows :
id database_name
1 stock_1
2 stock_2
-
master_database
是默认数据库,其凭据位于.env文件中) - The
master_database
is the default database, and its credentials are in the .env file)
用户将能够通过提交包含3个字段的表单来登录应用程序:
The users will be able to login to the app by submitting a form with 3 fields:
- database_name
- email
- password
例如,当用户想要连接到stock_1
数据库时,我将执行以下操作:
I do the following when the user wants to connect to stock_1
database for example :
1-我检查stock
表(在master_database
中)是否存在stock_1
数据库.
1 - I check if the stock_1
database exists in the stock
table (in the master_database
).
2-通过向此方法提供database_name
来动态更改数据库配置,从而连接到stock_1
数据库:
2 - I connect to stock_1
database by changing the database config dynamically by providing the database_name
to this method :
public static function connect($db)
{
// get default connection
$connections = \Config::get('database.connections');
$newConnection = $connections[\Config::get('database.default')];
$newConnection['database'] = $db;
$newConnection['username'] = 'root';
$newConnection['password'] = 'root';
\Config::set('database.connections.' . $newConnection['database'], $newConnection);
\Config::set('database.default', $newConnection['database']);
}
3-我在此数据库的users
表中签入email
&输入的password
是正确的,并且我登录了用户.
3 - I check in the users
table in this database if the email
& password
entered are correct, and I log in the user.
所以我的问题是:
-
此动态数据库配置会影响整个项目吗?
Does this dynamic database configuration affect the whole project ?
如果有许多用户试图同时连接到不同的数据库怎么办?这会奏效吗,否则他们都会对配置共享相同的最后修改?
What if there are many users trying to connect to different databases at the same time? Is this going to work or they'll all share the same last modification to the config?
在将每只股票保存在单独的数据库中时,还有更好的方法吗?
Is there a better way to do this while keeping each stock in a separate database?
推荐答案
这种动态数据库配置会影响整个项目吗?
Does this dynamic database configuration affect the whole project ?
是的,只要您在每次请求进入时都定义了此connect
方法即可.
Yes it is, as long as you have called this connect
method you defined every time the request came in.
如果有许多用户试图同时连接到不同的数据库怎么办?这会奏效吗,否则他们都将对配置共享相同的最后修改?
What if there are many users trying to connect to different databases at the same time? Is this going to work or they'll all share the same last modification to the config?
每次用户发出请求时,初始配置都是.env中的配置,直到您调用connect
方法为止.是的,即使有许多用户连接到您的服务器(只要他们在不同的请求中),它也不会中断.
Every time a user make a request, the initial config would be the one from .env until you called your connect
method. So yeah, it won't break even there are many users connect to your server (as long as they are in different requests).
在将每只股票保存在单独的数据库中时,还有更好的方法吗?
Is there a better way to do this while keeping each stock in a separate database?
我认为您可以将connect方法放入中间件中,以便每当经过身份验证的用户提出请求时,您都可以连接到所需的数据库.
I think you can put your connect method in a middleware, so that you can connect to the desired database whenever an authenticated user make a request.
这篇关于Laravel同时动态连接到不同的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!