Monday, April 10, 2017

Migration in multiple databases laravel

I try to set multiple databases for run a migration. In the file "config/database.php" this is the code:

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'first_database'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    'mysql2' => [
        'driver' => 'mysql',
        'host' => env('DB_SEC_HOST', '127.0.0.1'),
        'port' => env('DB_SEC_PORT', '3306'),
        'database' => env('DB_SEC_DATABASE', 'second_database'),
        'username' => env('DB_SEC_USERNAME', 'root'),
        'password' => env('DB_SEC_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

In the file "env" this is the code:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=first_database
DB_USERNAME=root
DB_PASSWORD=

DB_SEC_CONNECTION=mysql2
DB_SEC_HOST=127.0.0.1
DB_SEC_PORT=3306
DB_SEC_DATABASE=second_database
DB_SEC_USERNAME=root
DB_SEC_PASSWORD=

When I run the migration with this code:

 Schema::connection('second_database')->create('test', function(Blueprint $table) {
        $table->increments('id');
    });

The terminal shows this error message:

[InvalidArgumentException]
Database [second_database] not configured.

What am I doing wrong?

Thanks!



via Mer

Advertisement