How To Add Foreign Key In Laravel 10 Migration

Websolutionstuff | May-05-2023 | Categories : Laravel

In this article, we will see how to add a foreign key in laravel 10 migration. Here, we will learn about laravel 10, add foreign key using migration. Laravel also provides support for creating foreign key constraints, which are used to force referential integrity at the database level.

So, let's see how to add a foreign key in laravel 10 migration, laravel 10 foreign key migration, how to set a foreign key in laravel 10, laravel drop foreign key, and laravel migration add a foreign key to an existing table.

For example, let's define a user_id column on the posts a table that references the id column on a users table.

Example:

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('body');
            $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');
    }
}

Example:

Schema::create('comments', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')->constrained();
    $table->foreignId('post_id')->constrained();
    $table->text('comment');
    $table->timestamps();
});

 


You might also like:

Recommended Post
Featured Post
How to Generate Fake Data using Tinker in Laravel 11
How to Generate Fake Data usin...

Hello, laravel web developers! In this article, we'll see how to generate fake data using Tinker in laravel 11....

Read More

May-22-2024

Laravel 11 Integrate Authorize.net Payment Gateway
Laravel 11 Integrate Authorize...

Hello, laravel web developers! In this article, we'll see how to integrate authorize.net payment gateway in laravel...

Read More

Sep-02-2024

How to Send Bulk Mail Using Queue in Laravel 8
How to Send Bulk Mail Using Qu...

In this article, we will see how to send bulk mail using a queue in laravel 8. Laravel queue is used for sending bu...

Read More

Feb-10-2021

How To Generate PDF File In Laravel 10
How To Generate PDF File In La...

In this article, we will see how to generate a pdf file in laravel 10. Here, we will learn about laravel 10 ge...

Read More

Mar-10-2023