One to Many & One to One Polymorphic Relationships - Laravel Eloquent Relationships (phần 2)

Ví dụ 1: Tao database taggables

C:\xampp\htdocs\hanam.com\database\migrations\2020_08_12_035856_create_taggables_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTaggablesTable extends Migration {
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {
    Schema::create('taggables', function (Blueprint $table) {
      $table->bigInteger('tag_id');
      $table->bigInteger('taggable_id');
      $table->string('taggable_type');
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::dropIfExists('taggables');
  }
}

C:\xampp\htdocs\hanam.com\app\Post.php

C:\xampp\htdocs\hanam.com\routes\web.php

Ví dụ 2: Tao database videos

C:\xampp\htdocs\hanam.com\routes\web.php

C:\xampp\htdocs\hanam.com\app\Post.php

C:\xampp\htdocs\hanam.com\app\Tag.php

C:\xampp\htdocs\hanam.com\app\Video.php

C:\xampp\htdocs\hanam.com\database\migrations\2020_08_12_035856_create_taggables_table.php

Ví dụ 3: Bỏ taggable_type đầy đủ sang dạng khuyết :)

C:\xampp\htdocs\hanam.com\routes\web.php

C:\xampp\htdocs\hanam.com\app\Providers\AppServiceProvider.php

File Database

Last updated

Was this helpful?