Join WhatsApp ChannelJoin Now

Laravel Custom Foreign Key Name Migration

In this article, we will show you Laravel Custom Foreign Key Name Migration. This article will give you simple example of Laravel Custom Foreign Key Name Migration. you will learn Laravel Custom Foreign Key Name Migration.

This article will give you simple example of Laravel Custom Foreign Key Name Migration of how to add custom foreign key name in laravel 6, laravel 7 and laravel 8 version.

Step 1 :- Create Migration Table command

php artisan make:migration create_comments_table

database/migrations/2021_04_14_064338_create_comments_table.php

<?php

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

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('user_id')->unsigned();
            $table->bigInteger('post_id')->unsigned();
            $table->text('comment');
            $table->timestamps();
              
            $table->foreign('user_id', 'comment_user_id_foreign')->references('id')->on('users');
            $table->foreign('post_id', 'comment_post_id_foreign')->references('id')->on('posts');
        });
    }

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

Step 2 :- run migration

php artisan migrate

Laravel Custom Foreign Key Name Migration

Recommended Posts