Laravel – How to remove foreign key constraint using migration?

Laravel – How to remove foreign key constraint using migration?

In this post we will give you information about Laravel – How to remove foreign key constraint using migration?. Hear we will give you detail about Laravel – How to remove foreign key constraint using migration?And how to use it also give you demo for it if it is necessary.

Sometimes it can be agreeable to remove a database column that hosts a foreign key relationship, and we add wrong column with foreign key constraint on table, At that time we must remove that column from table.

But we can’t remove directly using dropColumn() because we did apply foreign key constraint so we should drop foreign key constraint of that column using dropForeign() and then we can delete column using dropColumn(). You can see as bellw migration, first i added migration with wrong column then other migration for remove that column.

Wrong Column Migration:

Schema::create('admins', function (Blueprint $table) {

$table->increments('id');

$table->string('fullname');

$table->string('email')->unique();

$table->string('imagepath');

$table->string('password', 60);

$table->integer('post_id')->unsigned();

$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');

$table->timestamps();

});

Drop Column Migration:

Schema::table('admins', function (Blueprint $table) {

$table->dropForeign('admins_post_id_foreign');

$table->dropColumn('post_id');

});

Hope this code and post will helped you for implement Laravel – How to remove foreign key constraint using migration?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  58  =  64

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US