vendredi 21 juin 2019

Schema connection can not link to non default database

I have two sql server database. I try to add column to second database(sqlsrv2) by using migrate. I had try Schema::connection but it always link to the default database(sqlsrv). Below provided my code. Please advice. thanks.

In .env file

DB_CONNECTION=sqlsrv
DB_HOST=hostname1
DB_PORT=1433
DB_DATABASE=database1
DB_USERNAME=user1
DB_PASSWORD=123

DB_CONNECTION_SECOND=sqlsrv
DB_HOST_SECOND=hostname2
DB_PORT_SECOND=1433
DB_DATABASE_SECOND=database2
DB_USERNAME_SECOND=user2
DB_PASSWORD_SECOND=123

In database.php

'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

        'sqlsrv2' => [
            'driver' => env('DB_CONNECTION_SECOND'),
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST_SECOND'),
            'port' => env('DB_PORT_SECOND'),
            'database' => env('DB_DATABASE_SECOND'),
            'username' => env('DB_USERNAME_SECOND'),
            'password' => env('DB_PASSWORD_SECOND'),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

In migrate file

public function up()
    {
        Schema::connection('sqlsrv2')->table('staff', function (Blueprint $table) {
            $table->string('system_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection('sqlsrv2')->table('staff', function (Blueprint $table) {
            $table->dropcolumn('system_id');
        });
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire