调用 Config::set 更改 Lumen 5.4 中的数据库? (使用 Eloquent)

Call Config::set to change database in Lumen 5.4? (Using Eloquent)

我在 Lumen 中有一个小应用程序,它应该根据请求处理多个数据库 URL。为了首先实现这一点,我创建了一个文件夹 /config/ 和一个名为 /config/database.php[ 的配置文件=24=]。这是代码,我添加了一些注释来说明我到底需要做什么。

<?php
return [
    'migrations' => 'home',
    'default' => 'home',
    'connections' => [
        // This is the default option and its needed to check if the "client" exists.
        'home' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'home',
            'username'  => 'root',
            'password'  => 'password',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],    

        // When the client is found in the "home" connection, I'd like to continue with this database settings as default.
        'client' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'client_', // There is one database per client which in the end will look like this: "client_******", so I have to add this using Config:set...
            'username'  => 'root',
            'password'  => 'password',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
    ],
];

我必须打电话给 Config::set 来更改这些设置,但它似乎在 Lumen 上不可用。我如何更改这些设置(默认数据库连接及其数据库

我希望有办法让这成为可能。非常感谢:)

在查询中你可以使用如下:

$connectionClient = DB::connections('client'); $connectionHome = DB::connections('home');

希望对您有所帮助。

我找到了解决方案。

要设置和编辑您的配置,请使用 "config" 辅助方法并将数组作为参数。

示例:

config(['database.default' => 'your_connection']);