简体   繁体   中英

Laravel Update an external database column in migration

I have tried the following

class ...migration

    public function up()
    {
    //
    $connections = \App\AppConnectionsModel::get();
    foreach ($connections as $connection){
        ///set the default configurations
        Config::set('database.connections.inspection.database', $connection->database);
        Config::set('database.connections.inspection.port', $connection->port);
        Config::set('database.connections.inspection.username', $connection->username);
        Config::set('database.connections.inspection.password', $connection->password);
        Config::set('database.connections.inspection.host', $connection->host);


        //then get db and change the column tables
        Schema::table('tbl_users', function(Blueprint $table) {
            $table->string('password')->default('n/a');

        });


    }

}

What am trying to do is to add a column tbl_users in an external database table.

What else do i need to add for this to work ?

I have also tried

$connections = \App\AppConnectionsModel::get();
    foreach ($connections as $connection){
        //update user
       ...set config as above
        Schema::connection('inspection')->table('tbl_user',function(Blueprint $table){
            $table->string('password')->unsigned()->nullable();
        });
    }

Create a new database entry in config/database.php . For example a sqlite DB I use on a site:

'connections' => [

    'sqlite' => ['driver'   => 'sqlite',
                 'database' => database_path('database.sqlite'),
                 'prefix'   => '',],

You can give it any name you like, it hasn't have to be sqlite . For using another DB apart from the default, use \\DB::connection('yourDBname')->...

When all DB connections are set in the config, make an array with your connection names and loop them from there.

If you need a Schema for migrations, use \\Schema::setConnection('yourDBname')->create('table_name', function(Blueprint $table) {...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM