tyr*_*yro 4 php mysql database multi-tenant laravel
I have the master database with login table and corresponding database settings for each user. On login I should dynamically change the db settings fetching from the table. I can change the db connection but this is not persisting.
Config::set("database.connections.mysql", [
'driver' => 'mysql',
"host" => $usr_host,
"database" => $usr_database,
"username" => $usr_username,
"password" => $usr_password,
...
]);
Run Code Online (Sandbox Code Playgroud)
edit: New database is created for each user when he/she registers with the app and thus i dont have the database connection for each user defined in the config/database.php
这样,您可以在涉及数据库时设置新参数:
\Config::set('database.connections.mysql.database', $schemaName);
Run Code Online (Sandbox Code Playgroud)
记住PURGE要保留此设置
DB::purge('mysql');
Run Code Online (Sandbox Code Playgroud)
干杯!
那么您可以使用默认数据库进行用户登录,并为数据库名称添加一个新字段。然后,每当您需要查询不同的数据库时,您只需更改数据库连接即可。
像这样的东西
$someModel = new SomeModel;
$databaseName = "mysql2"; // Dynamically get this value from db
$someModel->setConnection($databaseName);
$something = $someModel->find(1);
Run Code Online (Sandbox Code Playgroud)
你可以在这里读更多关于它的内容。 http://fideloper.com/laravel-multiple-database-connections
| 归档时间: |
|
| 查看次数: |
5046 次 |
| 最近记录: |