Một điều đặc biệt chú ý là khi tạo khóa phụ thì bảng phục phải được sinh ra trước, migration (ok)

Đọc tham khảo bản full

https://app.gitbook.com/o/-LVfVRgJVnpA6bttpEFT/s/-MCBeDUD-PK_RF-YgJRe/

C:\xampp\htdocs\test\database\migrations\2022_05_10_163113_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->bigIncrements('id'); 
      $table->string("name");
      $table->foreignId('test_id')->constrained('testss');
      // $table->foreignId('test_id')->constrained();
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::dropIfExists('posts');
  }
}

C:\xampp\htdocs\test\database\migrations\2022_05_09_022308_create_testss_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTestssTable extends Migration {
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {
    Schema::create('testss', function (Blueprint $table) {
      $table->bigIncrements('id');
      $table->string('name');
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::dropIfExists('testss');
  }
}

Last updated