Laravel 6 config()-> get('database.connections.mysql')与DB:connection()不匹配 [英] Laravel 6 config()->get('database.connections.mysql') not matching DB:connection()

查看:141
本文介绍了Laravel 6 config()-> get('database.connections.mysql')与DB:connection()不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的本地环境中,我正在与多个租户和Redis(需要身份验证)一起工作.
为了服务该项目,我正在使用Valet.

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.

运行时,我得到了 tenant_foo 的预期连接结果(与Redis相同)

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

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

运行时,我得到了 basic_foo 的错误但显然有效的结果

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

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

因此所有应用程序都将返回此Illuminate \ Database \ QueryException

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...

应在何处搜索

'tenant_foo.table_bar'

尚未解决问题的事物

  • 重新启动Redis
  • 重新安装Redis
  • php artisan config:cache
  • php artisan cache:clear
  • php artisan route:clear
  • php artisan view:clear
  • php artisan优化
  • composer dump-autoload
  • 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
    • 仅按如下所示将数据库名称更改为 tenant_foo 是不够的,因为config数组与 basic_foo 相同.

      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');
      

      想法

      • 我想更改 \ DB :: connection()的配置数组,但是除了config-> set()之外,我不知道其他方法.
      • 我安装了Telescope会影响数据库连接吗?
      • 还有其他想法吗?
      • 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?
        • 推荐答案

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

          To dynamically change database name you should use:

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

          这篇关于Laravel 6 config()-> get('database.connections.mysql')与DB:connection()不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆