Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







Foreign Key Migrations Simplified In Laravel 7.x | StackCoder


Foreign Key Migrations Simplified In Laravel 7.x


08th August 2020 2 mins read
Share On     Share On WhatsApp     Share On LinkedIn


In this article, we will see how to create foreign key columns in Laravel 7.x migrations very easily.


Before Laravel 7.x Creating Migrations


In earlier versions of Laravel creating foreign key constraints were easy but too much of syntax.


customer

public function up()
{
    Schema::create('customers', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('phone');
        $table->string('email')->nullable();

        /** Added By Foreign Keys */
        $table->unsignedBigInteger('added_by');
        $table->foreign('added_by')->references('id')->on('users');

        /** Update By Foreign Keys */
        $table->unsignedBigInteger('updated_by')->nullable();
        $table->foreign('updated_by')->references('id')->on('users');

        $table->timestamps();
        $table->softDeletes();
    });
}


Basically you have to setup the column and then declare the constraints.


Foreign Key Not Null Fields

/** Added By Foreign Keys */
$table->unsignedBigInteger('added_by');
$table->foreign('added_by')->references('id')->on('users');


Foreign Key Which Is Nullable

/** Update By Foreign Keys */
$table->unsignedBigInteger('updated_by')->nullable();
$table->foreign('updated_by')->references('id')->on('users');

Laravel 7.x Creating Foreign Keys


In Laravel 7.x the foreign key constraints adding is like breeze.


public function up()
{
    Schema::create('customers', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('phone');
        $table->string('email')->nullable();


        /** Added By Foreign Key */
        $table->foreignId('added_by')->constrained('users');


        /** Updated By Foreign Key */
        $table->foreignId('updated_by')->nullable()->constrained('users');


        $table->timestamps();
        $table->softDeletes();
    });
}


It's a very simple single liner now


Foreign Keys Not Nullable

/** Added By Foreign Key */
$table->foreignId('added_by')->constrained('users');


Foreign Keys With Nullable Column

/** Updated By Foreign Key */
$table->foreignId('updated_by')->nullable()->constrained('users');

Dropping Foreign Keys In Laravel 7.x Migrations


If you no longer need any foreign keys then you can easily delete them using the following migrations


public function up()
{
    Schema::table('customers', function (Blueprint $table) {
        $table->foreignId('added_by')->constrained('users');
        $table->foreignId('updated_by')->constrained('users');
    });
}

public function down()
{
    Schema::table('customers', function (Blueprint $table) {
        $table->dropForeign(['added_by']);
        $table->dropForeign(['updated_by']);
        $table->dropColumn(['added_by', 'updated_by']);
    });
}


First you need to delete the Foreign keys constraints, make sure to add separately.

$table->dropForeign(['added_by']);
$table->dropForeign(['updated_by']);


Then drop the columns

$table->dropColumn(['added_by', 'updated_by']);

Conclusion


Hope this article helped you. Please share it with your friends.




Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing