-1

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');
        });
    }
kiong
  • 7
  • 2
  • Have you tried with php artisan migrate --database=**otherDatabase*? – Dhananjay Kyada Jun 21 '19 at 11:13
  • @kiong code looks alright. It should work. refer to [this](https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel) article. It may help you. – Mike Ross Jun 21 '19 at 13:24
  • I'm puzzled that both sqldb's refer to the same URL. Is that your intention? It is not the db_connection that matters, this refers to the driver you are using. But if you refer to the same URL, you will end up into the same database. – Dimitri Mostrey Jun 22 '19 at 03:43

2 Answers2

-1

I know this may be a little bit simple and youve probably already tried this but doing a

php artisan optimize or php artisan config:cache

May either show you a problem somewhere or just refresh the configuration cache.

This is a very simple command i'm sure you have tried but 99% of the time I have weird problems like this I just run these types of commands and the problems solved.

Hope this helps in some way! good luck!

Andrew Gosselin
  • 153
  • 1
  • 16
-1

Dhananjay Kyada. thanks of suggestion.

I had tried php artisan optimize or php artisan config:cache. It can not work.

Only this work for me: php artisan migrate --database=sqlsrv2

kiong
  • 7
  • 2