How to Drop Foreign Key Constraint in Laravel Migration?
Hi Artisan,
This post will give you an example of laravel migration remove foreign key. This post will give you a simple example of laravel migration drop foreign key. If you have a question about remove foreign key constraint laravel then I will give a simple example with a solution. I’m going to show you about laravel migration remove foreign key constraint.
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. here, we will remove foreign key constraint using migration in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.
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.
Example: Laravel Add Foreign Key using Migration
Create Migration Command:
php artisan make:migration create_posts_table
database/migrations/2021_04_01_040458_create_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('body');
$table->timestamps();
});
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('post_id');
$table->text('comment');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('post_id')->references('id')->on('posts');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('comments');
Schema::dropIfExists('posts');
}
}
run migration
php artisan migrate
Output:
Example: Laravel Drop Foreign Key using Migration
Create Migration Command:
php artisan make:migration drop_posts_table
database/migrations/2021_04_01_040458_drop_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->dropForeign(['user_id', 'post_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
run migration
php artisan migrate
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Migration Add Column After Column Example
- Laravel Migration Default Value Current Timestamp Example
- How to Run Migration and Seeder on Laravel Vapor?
- How to Change Column Length using Laravel Migration?
- How to Update Enum Value in Laravel Migration?
- Laravel Migration Add Enum Column Example
- Laravel Migration Add Comment to Column Example
- Laravel Migration Custom Index Name Example
- How to Add Foreign Key in Laravel Migration?
- Laravel Migration - How to Add New Column in Existing Table ?
- How to Change Table Name using Laravel Migration?
- How to Remove Column from Table in Laravel Migration?
- How to Change Column Name and Data Type in Laravel Migration?
- How to Create Table using Migration in Laravel?
- How to Add MySQL Trigger from Migration in Laravel?