Laravel - 全局更改默认数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18975500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:28:28  来源:igfitidea点击:

Laravel - change the default database connection globally

phplaravel

提问by ericbae

In my database.php, I have TWO databases configured.

在我的database.php.NET 中,我配置了两个数据库。

'db1' => array(
  'driver'   => 'pgsql',
  'host'     => 'localhost',
  'database' => 'db1',
  'username' => 'root',
  'password' => 'password',
  'charset'  => 'utf8',
  'prefix'   => '',
  'schema'   => 'public',
), 

'db2' => array(
  'driver'   => 'pgsql',
  'host'     => 'localhost',
  'database' => 'db2',
  'username' => 'root',
  'password' => 'password',
  'charset'  => 'utf8',
  'prefix'   => '',
  'schema'   => 'public',
),

So by default db1is set as default DB initially. Now I want to switch the default database to 'db2' by selecting an option from 'select' dropdown. This will do a post AJAX request to the controller method in which I do

所以默认情况db1下最初设置为默认数据库。现在我想通过从“选择”下拉列表中选择一个选项来将默认数据库切换到“db2”。这将对我在其中执行的控制器方法执行后置 AJAX 请求

public function postChangeDb()  {
    $db = Input::get('db');
    Config::set('database.default', $db);
}

Once this is done, I 'refresh' the page, but the connection is still at 'db1'.

完成后,我“刷新”了页面,但连接仍位于“db1”。

I also tried the following

我也尝试了以下

  public function getTest() {
    Config::set('database.default', 'db1');
    $users = User::all();
    echo sizeof($users); // returns 20

    Config::set(database.default', 'db2');
    $users = User::all();
    echo sizeof($users); // returns 50 - which is correct!
  }

And the above works fine and it successfully switches the database. Is the switch 'per request' basis?

以上工作正常,成功切换数据库。开关是“每个请求”的基础吗?

回答by J.T. Grimes

Config::setis only going to work on a per-request basis, so you're probably going to want to set your database in the Session and grab it on subsequent requests.

Config::set只会在每个请求的基础上工作,因此您可能希望在 Session 中设置您的数据库并在后续请求中获取它。

You have some options on where to do that. /app/start/globalwould be one option. In the controller constructor would be another. You could register a service provider to do it, too.

你有一些关于在哪里做的选择。 /app/start/global将是一种选择。在控制器构造函数中将是另一个。您也可以注册服务提供商来执行此操作。

Below is an example of what the code might look like [warning: untested code!] in the controller constructor:

下面是控制器构造函数中代码可能看起来像 [警告:未经测试的代码!] 的示例:

   public function __construct() {
     if(Session::has('selected_database'){
        Config::set('database.default',Session::get('selected_database'));
     } else {
        return Redirect::to('database_choosing_page');
     }
   }

and an update to your database setting function:

以及对数据库设置功能的更新:

public function postChangeDb()  {
    $db = Input::get('db');
    Session::put('selected_database',$db);
    Config::set('database.default', $db);
}

回答by Sergey Telshevsky

Have you tried simply to change the default connection in app/config/database.php?

您是否尝试过简单地更改 中的默认连接app/config/database.php

'default' => 'db2'

If it's not the case then please provide more information on the problem.

如果不是这种情况,请提供有关问题的更多信息。

Edit:So it seems you have all connections hardcoded in the models. Try updating the models like that:

编辑:因此,您似乎在模型中硬编码了所有连接。尝试像这样更新模型:

    protected $connection = 'db2';