Search code examples
phplaravellaravel-5.3

Laravel 5.3 multi database


This maybe common question, but i've been stuck for 2 days T_T

it's pretty simple,

  • I create a new laravel 5.3 project
  • I remove db config in .env file
  • I define 2 x db connections in config/database.php as follow:

    'connections' => [
        'web' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'port' => '3306',
            'database' => 'db_host1',
            'username' => 'username1,
            'password' =>  'secret1',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
        'another' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'port' => '3306',
            'database' => 'db_host2',
            'username' => 'username2',
            'password' =>  'secret2',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
    ]
    

    it's clear and straight forward

  • php artisan make auth

  • modify migrations as follow:

    class CreateUsersTable extends Migration{
        //bla....
        public function up(){
              Schema::connection('another')->create('users', function (Blueprint $table) {
                    //bla
              }
          //bla
        }
    
    class CreatePasswordResetsTable extends Migration{
        //bla....
        public function up(){
            Schema::connection('another')->create('password_resets', function (Blueprint $table) {
            //bla
        }
        //bla
    }
    

    it's clear and straight forward

  • php artisan migrate - sweet, db created in a right place

  • goto registercontroller.php and modify create method as follow:

    protected function create(array $data){
      return User::create([
          'connection' => 'another',
          'name' => $data['name'],
          'email' => $data['email'],
          'password' => bcrypt($data['password']),
      ]);
    }
    
  • I also tried to put connection in app/User.php as follow:

    class User extends Authenticatable{
        use Notifiable;
        protected $connection = 'another';
    
  • test to register user - fill up all form fields and submit

  • DANG! error comeout:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'web.users' doesn't exist (SQL: select count(*) as aggregate from users where email = [email protected])

question: why it still look up from 'web' DB connection as i have told to use 'another' DB connection T_T ?

am i missed anything here?

i have read a couple doc like: http://fideloper.com/laravel-multiple-database-connections

but it doesnt help me...

appreciate if someone could help me here :)

cheers,


Solution

  • for the validation part. Though you changed the connection to another in the validation part your still looking out for web connection users table just replace the following code in RegisterController and you are good to go.

    return Validator::make($data, [
                'name' => 'required|max:255',
                'email' => 'required|email|max:255|unique:another.users',
                'password' => 'required|min:6|confirmed',
            ]);
    

    unique:another.users *This part is important as it tells which connection to be used and the table for the field under validation.

    credits: laracast