【问题标题】:change Laravel default user db connection to another db connection将 Laravel 默认用户数据库连接更改为另一个数据库连接
【发布时间】:2016-09-22 15:08:22
【问题描述】:

我的 config/database.php 如下:

'default' => 'web',

'connections' => array(

# Our primary database connection
'web' => array(
    'driver'    => 'mysql',
    'host'      => 'host1',
    'database'  => 'database1',
    'username'  => 'user1',
    'password'  => 'pass1'
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

# Our secondary database connection
'another' => array(
    'driver'    => 'mysql',
    'host'      => 'host2',
    'database'  => 'database2',
    'username'  => 'user2',
    'password'  => 'pass2'
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

),

我正在使用标准 laravel 5.3 用户身份验证。

我在我的 registercontroller.php 中尝试过:

protected function create(array $data)
{

    $user = new user();
    $user->setConnection('another');
    ...
}

仍然没有运气

如果我想为与用户相关的任何事情(例如登录、注册、忘记密码等)将数据库连接移动或更改为“另一个”数据库连接,我需要更改的设置在哪里?

我只是尝试将我的 config/database.php 默认连接更改如下:

    'default' => 'another',

它有效,所以我似乎需要告诉/配置某处为任何用户事务使用“另一个”数据库连接

谢谢!

【问题讨论】:

  • Check the doc。另外,检查/config/auth.php 文件。
  • @TheAlpha 感谢 cmets,但它仍然没有给我敲响警钟...我在那里看不到任何 sb 连接设置...
  • 为什么要为用户使用不同的数据库连接?

标签: php database laravel laravel-5.3


【解决方案1】:

要仅对特定模型使用不同的连接,您可以在模型类中设置一个属性,例如应用\用户:

class User extends Authenticatable
{
    use Notifiable;

    protected $connection = 'another';
 ...
}

https://laravel.com/docs/5.3/eloquent#basic-usage

【讨论】:

    【解决方案2】:

    转到app>auth.php

    并编辑表格配置:

     'passwords' => [
            'users' => [
                'provider' => 'users',
                'table' => 'your_table_here',
                'expire' => 60,
            ],
        ],
    

    【讨论】:

      猜你喜欢
      • 2013-09-29
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 2018-06-14
      • 2014-03-06
      • 2018-07-27
      相关资源
      最近更新 更多