将Laravel默认用户数据库连接更改为另一个数据库连接

时间:2016-09-22 15:08:23

标签: php database laravel laravel-5.3

我有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',

并且它有效,所以我似乎需要告诉/配置某个地方使用'另一个'任何用户事务的数据库连接

谢谢!

2 个答案:

答案 0 :(得分:0)

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

class User extends Authenticatable
{
    use Notifiable;

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

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

答案 1 :(得分:0)

转到app>auth.php

并编辑表格配置:

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