AnD
AnD

Reputation: 3129

change Laravel default user db connection to another db connection

I have config/database.php as follow:

'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'    => '',
),

),

and i'm using standard laravel 5.3 user authentication.

i tried in my registercontroller.php:

protected function create(array $data)
{

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

and still no luck

if i like to move or change the db connection into 'another' db connection for anything related with user (e.g. login, register, forgot pass, etc.), where is the settings that i need to change?

I just tried to change my config/database.php default connection as follow:

    'default' => 'another',

and it works, so it seems like i need to tell/configure somewhere that to use 'another' DB connection for any user transaction

Thanks!

Upvotes: 3

Views: 6552

Answers (2)

Led
Led

Reputation: 517

go to app>auth.php

and edit the table configuration:

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

Upvotes: 0

macghriogair
macghriogair

Reputation: 1481

To use a different connection for specific models only, you can set a property in your Model class, e.g. App\User:

class User extends Authenticatable
{
    use Notifiable;

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

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

Upvotes: 2

Related Questions