Laravel 6 `config()->get(‘database.connections.mysql’)` 与 DB:`connection()` 不匹配。

huangapple go评论74阅读模式
英文:

Laravel 6 config()->get('database.connections.mysql') not matching DB:connection()

问题

先决条件

在我的本地环境中,我正在处理多个租户和Redis(需要身份验证)。
为了提供项目,我正在使用Valet。

对于这种情况,我正在处理这两个连接:

  • basic_foo(在我的.env中定义)
  • tenant_foo(在请求期间要更改的连接)

到目前为止,我已经成功地像这样更改了连接:

config()->set('database.connections.mysql', 
    array_merge(
        config()->get('database.connections.mysql'),
        ['database' => 'tenant_foo']
    )
);

问题

然而,现在我看到了查询构建器的问题,它保持或回退到基本连接。

当我运行以下代码时,我得到了预期的连接结果 tenant_foo(对于Redis也是一样):

dd(config()->get('database.connections.mysql'));

但是当我运行以下代码时,我得到了错误但明显是活动的 basic_foo 结果:

dd(\DB::connection()); // 返回 Illuminate\Database\MySqlConnection

所以总体而言,应用程序将返回以下 Illuminate\Database\QueryException:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'basic_foo.table_bar' doesn't exist...

它应该搜索:

'tenant_foo.table_bar'

尚未解决问题的事情

  • 重新启动Redis
  • 重新安装Redis
  • php artisan config:cache
  • php artisan cache:clear
  • php artisan route:clear
  • php artisan view:clear
  • php artisan optimize
  • composer dump-autoload

仅仅将数据库名称更改为 tenant_foo 不足够,因为配置数组仍然是 basic_foo

\DB::connection()->setDatabaseName('tenant_foo');

想法

  • 我想更改 \DB::connection() 的配置数组,但我不知道除了使用 config->set() 之外是否还有其他方法。
  • 我安装了Telescope,这可能会影响数据库连接吗?
  • 有其他想法吗?
英文:

Prerequisites

In my local environment I am working with multiple tenants and Redis (Auth required).
To serve the project I am using Valet.

For this case I am addressing these two connections:

- basic_foo (is defined in my .env)
- tenant_foo (is the one to change to during a request)

Until now I successfully changed the connections like so:

config()->set('database.connections.mysql', 
       array_merge(
        config()->get('database.connections.mysql') , 
        ['database' => 'tenant_foo']
    ); 

Problem

However, now I am seeing an issue with the query builder, keeping or falling back to the basic connection.

I get the expected connection results of tenant_foo (same for Redis) when I run

dd(config()->get('database.connections.mysql'));

I get the wrong but apparently active results of basic_foo when I run

dd(\DB::connection()); // returns Illuminate\Database\MySqlConnection

So all in all the app will return this Illuminate\Database\QueryException

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'basic_foo.table_bar' doesn't exist...

where it should search for

'tenant_foo.table_bar'

Things that did not solve the problem yet

  • restarting Redis
  • reinstalling Redis
  • php artisan config:cache
  • php artisan cache:clear
  • php artisan route:clear
  • php artisan view:clear
  • php artisan optimize
  • composer dump-autoload

Simply changing the database name to tenant_foo like below is not enough, as the config array remains the same of basic_foo.

\DB::connection()->setDatabaseName('tenant_foo');

Thoughts

  • I want to change the config-array the of \DB::connection(), but I don't know another way than the config->set().
  • I installed Telescope could this affect the db connection?
  • Any other ideas?

答案1

得分: 2

为了动态更改数据库名称,您应该使用:

DB::disconnect();
Config::set('database.mysql.database', 'tenant_foo');
DB::reconnect();
英文:

To dynamically change database name you should use:

DB::disconnect(); 
Config::set('database.mysql.database', 'tenant_foo'); 
DB::reconnect();

答案2

得分: 2

这对我有效:

\DB::disconnect('mysql');

Config::set('database.connections.mysql.database', 'tenant_foo');

\DB::reconnect('mysql');
英文:

This worked for me:

\DB::disconnect('mysql'); 

Config::set('database.connections.mysql.database', 'tenant_foo');

\DB::reconnect('mysql');

huangapple
  • 本文由 发表于 2020年1月3日 23:13:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定