One to Many & One to One Polymorphic Relationships - Laravel Eloquent Relationships (phần 1)
Ví dụ 1: Tạo dữ liệu cho comments, posts, users sử dụng morphMany, morphs, morphTo
Bức ảnh sau giải thích ta sử dụng morphs để xử lý nhanh 2 bảng commentable_id, commentable_type :)


C:\xampp\htdocs\hanam.com\routes\web.php
Route::get('/', function () {
$user = \App\User::create([
'name' => 'Lionel',
'email' => 'phamkyanh8668@gmail.com',
'password' => Hash::make('password'),
]);
$post = \App\Post::create([
'user_id' => $user->id,
'title' => "Title example 1",
]);
$post->comments()->create([
'user_id' => $user->id,
'body' => 'Comment For Post 1'
]);
return view('welcome');
});
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');
}
}
C:\xampp\htdocs\hanam.com\app\Comment.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model {
protected $guarded = [];
public function commentable() {
return $this->morphTo();
}
}
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');
}
}
C:\xampp\htdocs\hanam.com\database\migrations\2020_08_10_143945_create_comments_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCommentsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->text('body');
$table->bigInteger('user_id');
// $table->bigInteger('commemtable_id');
// $table->string('commemtable_type');
$table->morphs('commentable');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('comments');
}
}
C:\xampp\htdocs\hanam.com\database\migrations\2020_08_10_143922_create_videos_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateVideosTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('videos', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('videos');
}
}
Ví dụ 2: Sử dụng morphTo

C:\xampp\htdocs\hanam.com\app\Comment.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model {
protected $guarded = [];
public function commentable() {
return $this->morphTo();
}
}
C:\xampp\htdocs\hanam.com\routes\web.php
$comment = \App\Comment::find(3);
dd($comment->commentable);
return view('welcome');
Kết quả: tìm được dữ liệu của comment 3 là trong video

Ví dụ 3: Sử dụng morphOne
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();
}
}
C:\xampp\htdocs\hanam.com\routes\web.php
$post = \App\Post::find(1);
dd($post->comment);
return view('welcome');

Previoushas-one-through & has-many-through Laravel Eloquent Relationships (phần 2)NextOne to Many & One to One Polymorphic Relationships - Laravel Eloquent Relationships (phần 2)
Last updated
Was this helpful?