database - 在 Lumen 5.4 中调用 Config::set 来更改数据库? (使用 Eloquent )

标签 database laravel lumen

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

<?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 上不可用。我如何更改这些设置(默认数据库连接 及其数据库)

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

最佳答案

我找到了解决方案。

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

例子:

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

关于database - 在 Lumen 5.4 中调用 Config::set 来更改数据库? (使用 Eloquent ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45851189/

相关文章:

mysql - SQL 数据库架构建模 : Vehicle with two different type owners

ruby-on-rails - Rails 按标题、正文和标签搜索帖子

laravel - 如何在Safari中播放音频文件而不让任何人直接下载它?

Laravel 8 部署在 Ubuntu 上,但不断收到 : Undefined variable

php - 如何在laravel中获取最后插入的id

php - Laravel 使用 XML 测试 POST 路由

Android - 指南 - 带有在线数据库的应用程序

mysql - 当在数据库中删除某些内容时,如何更改其他记录以便记录的 id 发生变化

php - 流明中的时区问题

php - 1 个具有关系的多个数据库的表单