How to Drop Foreign Key in Laravel 10 Migration

Greetings, fellow developers! Have you ever been knee-deep in your Laravel project, working with database relationships, and needed to bid farewell to a foreign key? In this step-by-step walkthrough, we'll explore the process of dropping a foreign key from your database table.

So, let's see how to drop the foreign key in laravel 10 migration, laravel 10 drops the foreign key using migration, drops the foreign key in MySQL, and drops the foreign key column in laravel 8/9/10.

let's unravel the mysteries of dropping foreign keys in Laravel migrations. Ready? Let's dive in! 🚀

Example: Laravel Add Foreign Key

We'll create migration using the following command.

php artisan make:migration create_posts_table

Migration:

<?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('description');
            $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

 

Example: Laravel Drop Foreign Key

Start by creating a migration using the Artisan command:

php artisan make:migration drop_posts_table

Navigate to the newly created migration file in the database/migrations directory and open it. Add the code for dropping the foreign key within the up method.

<?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 the Migration:

Execute the migration to apply the changes to your database:

php artisan migrate

And there you have it! You've successfully learned how to drop a foreign key in Laravel 10 migrations

Happy coding! 🚀.

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS