How I drop multiple foreign key and primary keys in migration file.

Bellow is my migration file code.

Migration File

public function up()
{
    Schema::create('role_user', function(Blueprint $table){
        $table->integer('role_id')->unsigned();
        $table->integer('user_id')->unsigned();

        $table->foreign('role_id')
                ->references('id')
                ->on('roles')
                ->onDelete('cascade');

        $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

        $table->primary(['role_id', 'user_id']);
    });
}
public function down()
{
    Schema::drop('role_user');
    //how drop foreign and primary key here ?
}

1 Answer 正确答案

Blueprint class offers dropForeign and dropPrimary methods that allow you to remove foreign key constraints and primary key.

The following should do the trick:

public function down()
{
    Schema::table('role_user', function (Blueprint $table) {
        $table->dropForeign('role_user_role_id_foreign');
        $table->dropForeign('role_user_user_id_foreign');
        $table->dropPrimary();
    });
}

来自  https://stackoverflow.com/questions/39072220/how-i-drop-foreign-and-primary-key-of-table-in-migration