Php 在alter table迁移中将现有外键列设置为nullable

Php 在alter table迁移中将现有外键列设置为nullable,php,laravel,Php,Laravel,我首先创建了如下迁移: Schema::create('table1',function(Blueprint $table){ $table->bigIncrements('id'); $table->string('name')->unique(); $table->integer("user_id")->unsigned(); $table->foreign("user_id)->ref

我首先创建了如下迁移:

Schema::create('table1',function(Blueprint $table){
        $table->bigIncrements('id');
        $table->string('name')->unique();
        $table->integer("user_id")->unsigned();
        $table->foreign("user_id)->references("id")->on("users");
});
然后我想将nullable属性添加到user_id列,我编写了以下迁移:

Schema::table('f_subjects', function (Blueprint $table) {
        $table->integer('user_id')->nullable()->change();
        $table->foreign('original_law_id')->references('id')->on('f_original_law');
    });
Schema::table('table1',function(Blueprint $table){
    //Or disable foreign check with: 
    //Schema::disableForeignKeyConstraints();
    $table->dropForeign('table1_user_id_foreign');
    $table->integer('user_id')->nullable()->unsigned()->change();
    //Remove the following line if disable foreign key
    $table->foreign('user_id')->references('id')->on('users');
});
但我有一个错误:

Cannot change column 'user_id': used in a foreign key constraint 'table1_user_id_foreign'

1。首先需要删除约束:

$table->dropForeign(['user_id']);
2.或者您可以暂时禁用FK约束:

Schema::disableForeignKeyConstraints();
Schema::enableForeignKeyConstraints();
然后启用约束:

Schema::disableForeignKeyConstraints();
Schema::enableForeignKeyConstraints();

在迁移中始终使用以下代码:

 public function down()
    {

        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('table');
        Schema::enableForeignKeyConstraints();
    }

1-删除外键

$table->dropForeign('table1_user_id_foreign');
2-更改用户id列定义:

//If user_id is not unsigned remove unsigned function
$table->integer('user_id')->nullable()->unsigned()->change();   
3-创建索引

$table->foreign('user_id')->references('id')->on('users');
完全迁移:

Schema::table('f_subjects', function (Blueprint $table) {
        $table->integer('user_id')->nullable()->change();
        $table->foreign('original_law_id')->references('id')->on('f_original_law');
    });
Schema::table('table1',function(Blueprint $table){
    //Or disable foreign check with: 
    //Schema::disableForeignKeyConstraints();
    $table->dropForeign('table1_user_id_foreign');
    $table->integer('user_id')->nullable()->unsigned()->change();
    //Remove the following line if disable foreign key
    $table->foreign('user_id')->references('id')->on('users');
});

谢谢你们的回复,但我不能删除这个表,因为它有一些重要的数据,我必须传递一个数组到$table->dropForeign,就像下面的回答一样。我想这是因为拉威尔的一个更高版本。