How to Add Foreign Key in Laravel Migration? (ok)

https://www.itsolutionstuff.com/post/how-to-add-foreign-key-in-laravel-migrationexample.html

Ví dụ 1:

C:\xampp\htdocs\reset\database\migrations\2022_05_20_180539_create_posts_table.php

<?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');
  }
}

Ví dụ 2:

C:\xampp\htdocs\reset\database\migrations\2022_05_20_180539_create_posts_table.php

<?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');
    // });
    Schema::create('comments', function (Blueprint $table) {
      $table->id();
      $table->foreignId('user_id')->constrained();
      $table->foreignId('post_id')->constrained();
      $table->text('comment');
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::dropIfExists('comments');
    Schema::dropIfExists('posts');
  }
}

imHow to Add Foreign Key in Laravel Migration?

I will give you very simple example of how to create table with foreign key constraint using laravel migration. you can easily use this example with laravel 6, laravel 7, laravel 8 and laravel 9 version.

in this example, we will create "posts" and "comments" table. in comments table we will add two foreign key constraint one with posts and another with users table. so let's simple create migration and let's see:

Example 1:

Create Migration Command:

php artisan make:migration create_posts_table

database/migrations/2021_04_01_040458_create_posts_table.php

<?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');    }}

run migration

php artisan migrate

Output:

Example 2:

Read Also: How to use Laravel Model Observers?

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

I hope it can help you...

Last updated