如何在不同的数据库中迁移?

时间:2019-09-23 20:42:20

标签: laravel laravel-5

我有这个软件,我需要创建新的机构(例如幼儿园)。如何迁移到该新数据库?

if ($newDb) {
      Config::set('database.connections.th', [
        'driver' => 'mysql',
        'host' => 'localhost',
        'port' => '3306',
        'database' => 'kindergarden'.$institutionId,
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'timezone' => '+00:00',
        'strict' => false,
    ]);
    return \Illuminate\Support\Facades\Artisan::call('migrate');
    }

迁移就像

public function up()
{
    Schema::create('students', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('first_name', 100);
        $table->string('last_name', 100);
        $table->tinyInteger('gender');
        $table->date('date_of_birth');
        $table->string('address', 150);
        $table->string('phone_number', 20);
        $table->string('email', 100);
        $table->unsignedTinyInteger('status')->default(1);
        $table->timestamps();
        $table->softDeletes();
    });
}

1 个答案:

答案 0 :(得分:0)

在表迁移中设置非默认数据库连接

Schema::connection('th')->create('students', function (Blueprint $table)

这将通知artisan将此表迁移到您配置中的指定数据库

Docs

数据库连接和表选项

如果要对不是默认connection的数据库连接执行模式操作,请使用连接方法:

Schema::connection('foo')->create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
});
相关问题