什么是 down() 函数来逆转我所做的 Laravel 迁移?

What is the down() function to reverse this Laravel migration that I make?

所以,我想添加一列 ('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()
    {
     //
    }

我还想 drop/delete 我创建的这个外键 ('id'),因为它有时会在我的项目中出现约束错误。我的问题是在迁移失败或错误的情况下,我应该写什么函数来反转上面我将要做的更新?

这是我现在使用的 table“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 artisan make:migration 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