什么是down()函数来逆转我进行的Laravel迁移



所以,我想添加一列('Semester_TA'(,并将该列的属性('kode_mk'(更改为唯一值,所以我创建了这个迁移:

public function up()
{
Schema::table('mk', function(Blueprint $table){
$table->string('Semester_TA')->after('sks');
$table->string('kode_mk')->unique();
});
Schema::table('lr2', function(Blueprint $table){
$table->dropForeign(['id']);
});
}
public function down()
{
//
}

我还想删除我创建的这个外键('id'(,因为它有时会在我的项目中产生约束错误。我的问题是,如果迁移失败或出错,我应该写什么函数来反转上面的更新?

这就是桌子";mk";以及";lr2";我现在使用的迁移:

Schema::create('mk', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('kode_mk');
$table->string('mk');
$table->integer('sks');
$table->integer('semester');
$table->integer('available_seats');
$table->timestamps();
});
Schema::create('lr2', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('k_mk');
$table->string('mk');
$table->string('angkatan');
$table->integer('request_seats');
$table->boolean('status_request');
$table->timestamps();
$table->foreign('id')->references('id')->on('mk');
});

首先,您必须创建一个新的迁移:

php手工制作:迁移Add_column_change_the_property

然后在迁移文件up((中,尝试:

public function up()
{
Schema::table('mk', function(Blueprint $table){
$table->string('Semester_TA')->after('sks');
$table->string('kode_mk')->unique();
});
}

然后在迁移文件down((中尝试:

public function down()
{
$table->dropColumn('kode_mk');
$table->foreign('id')->references('id')->on('mk');
//
}

然后通过以下方式迁移文件:php artisan migrate

最新更新