Drop Foreign Key with Column in Laravel Migration Example

Laravel Drop Foreign key Existing Table Example; In this tutorial you will learn How to drop foreign key with column in laravel migration. Many times we need to drop a foreign key constraints from database column.

laravel dropForeign() function used to deleting any foreign key easily from the database table. We can remove only foreign key and with column also just need to add a new migration and update the functionality there.

Previous Table Migration:

Suppose you have a previous migration where you haved added the foreign key constraint just like below. Here the table invoices migration and where we check the ‘user_id’ have a foreign key.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateInvoicesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('invoices', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');
            $table->timestamps();

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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('invoices');
    }
}

Now we add a migration to removing the foreign key without any errors getting just the command and generate a migration.

php artisan make:migration add drop_user_id_to_invoices_table --table=invoices

Drop Foreign Key from Laravel Migration

Now we will add a new migration to drop the foreign key in laravel migration. Fir you need to generate a new migration using the following command:

php artisan make:migration drop_user_id_to_invoice_table

Now open the migration file and update the code according the below example:

Example 1:

You can remove the foreign key constaint using the below example:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class dropUserIdToInvoicesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('invoices', function (Blueprint $table) {
            $table->dropForeign('user_id');
            $table->dropColumn('user_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
       Schema::table('invoices', function (Blueprint $table) {
          $table->unsignedInteger('user_id');

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

Now run the migration command

php artisan migrate

The foreign key and its column its deleted from database table’s. If you run the rollback command its generated again.

If still this this example now working to remove the foreign key and getting error then you can add the below code. We can use [table]_[column]_foreign for removing any foreign key.

Example 2:

$table->dropForeign('invoices_user_id_foreign');
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class dropUserIdToInvoicesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('invoices', function (Blueprint $table) {
            $table->dropForeign('invoices_user_id_foreign');
            $table->dropColumn('user_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
         Schema::table('invoices', function (Blueprint $table) {
          $table->unsignedInteger('user_id')->nullable();

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

Example 3: Drop Foreign key and Index in Laravel

Sometimes both above two examples not working for dropping foreign key in laravel so you can use this example its definitely delete your foreign key and then you can drop your column as well.

public function up()
{
    Schema::table('invoices', function (Blueprint $table) {
        $table->dropForeign('invoices_user_id_foreign');
        $table->dropIndex('invoices_user_id_foreign');
        $table->dropColumn('user_id');
    });
}

Read Here: Create Foreign Key Constraint in Laravel Migration

So guys, today we lean how to remove foreign key with column in laravel using migration. If you have question feel free to draw a comment below. Follow us on twitter with more update’s.

Leave a Comment