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

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
  protected $fillable = ['user_id','title'];
  public function user() {
  	return $this->belongsTo(User::class)->withDefault([
  		'name' => 'Guest User'
  	]);
  }
  // public function tags() {
  // 	// return $this->belongsToMany(Tag::class,'post_tag','post_id','tag_id');
  // 	// return $this->belongsToMany(Tag::class)->withTimestamps();
  //   return $this->belongsToMany(Tag::class)
  //   ->using(PostTag::class)
  //   ->withTimestamps()
  //   ->withPivot('status');
  // }
  public function comments() {
    return $this->morphMany(Comment::class,'commentable');
  }
  public function comment() {
    // return $this->morphOne(Comment::class,'commentable');
    return $this->morphOne(Comment::class,'commentable')->latest();
  }
  public function tags() {
   return $this->morphToMany(Tag::class,'taggable');
  }
}

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

  //Part 1
  $post = \App\Post::create([
    'user_id' => 1,
    'title'   => "Title example 1",
  ]);
  $post->tags()->create([
    'name' => "Laravel"
  ]);
    //Part 2  
  $post= \App\Post::find(1);
  $tag = \App\Tag::create([
    'name' => "PHP"
  ]);
  $post->tags()->attach($tag);
  return view('welcome');

Ví dụ 2: Tao database videos

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

$video = \App\Video::find(1);
dd($video->tags);
return view('welcome');

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

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
  protected $fillable = ['user_id','title'];
  public function user() {
  	return $this->belongsTo(User::class)->withDefault([
  		'name' => 'Guest User'
  	]);
  }
  // public function tags() {
  // 	// return $this->belongsToMany(Tag::class,'post_tag','post_id','tag_id');
  // 	// return $this->belongsToMany(Tag::class)->withTimestamps();
  //   return $this->belongsToMany(Tag::class)
  //   ->using(PostTag::class)
  //   ->withTimestamps()
  //   ->withPivot('status');
  // }
  public function comments() {
    return $this->morphMany(Comment::class,'commentable');
  }
  public function comment() {
    // return $this->morphOne(Comment::class,'commentable');
    return $this->morphOne(Comment::class,'commentable')->latest();
  }
  public function tags() {
   return $this->morphToMany(Tag::class,'taggable');
  }
}

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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model {
  protected $fillable = [
    'name',
  ];
  // public function posts() {
  //   return $this->belongsToMany(Post::class, 'post_tag', 'tag_id', 'post_id');
  // }
  public function posts() {
  	return $this->morphedByMany(Post::class, 'taggable');
  }
}

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

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model {
	protected $guarded = [];
  public function comments() {
    return $this->morphMany(Comment::class,'commentable');
  }
  public function comment() {
    return $this->morphOne(Comment::class,'commentable');
  }
  public function tags() {
   	return $this->morphToMany(Tag::class,'taggable');
  }
}

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

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

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

 $tag = \App\Tag::find(1);
  dd($tag->posts);
  return view('welcome');

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

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Relations\Relation;
class AppServiceProvider extends ServiceProvider {
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register() {
    //
  }
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot() {
    Relation::morphMap ([
      'Post'  => \App\Post::class,
      'Video' => \App\Video::class,
    ]);
  }
}

File Database

Last updated