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