Laravel drop foreign key constraint


Laravel drop foreign key constraint – We can use migration to drop foreign key constraint using the artisan command. Here in this tutorial we are going to explain how you can drop foreign key constraint in Laravel.


Laravel drop foreign key constraint Using Migration | with full Example.

Let us understand how to drop foreign key constraint Using Migration.

Full example of drop foreign key using migration.

Now here i am going to explain how to drop foreign key step by step.

Migration File:-

Let’s look at a simple example.

<?php

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

class CreateUsersTable extends Migration
{
    public function up()
    {
	    Schema::create('songs', function (Blueprint $table) { 
            $table->increments('id'); 
            $table->integer('user_id'); 
            $table->string('title'); 
            $table->string('slug')->unique(); 
            $table->timestamps(); 
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 
        }); 
    }

    public function down()
    {
	    Schema::table('songs', function(Blueprint $table) {
            $table->dropForeign('user_id');
            
        });
    }
}

Run this migration through artisan command for create a database.

php artisan migrate

For delete:-

php artisan migrate:rollback

Advertisements

Add Comment

📖 Read More