-comment 表
-id
-type
-model
-relation_id 关联id
-article 表
-id
-content
-news 表
-id
-content
public function getTypeAttribute($value)
{
$map = [
1 => Article::class,
2 => News::class,
];
$type = $map[$value] ?? '';
return $this->attributes['type'] = $type;
}
public function commentable()
{
return $this->morphTo('commentable', 'model', 'relation_id', 'id');
}
- Article model 代码, news 代码同理
public function commentable()
{
return $this->morphMany(Comment::class, 'commentable', 'model', 'relation_id', 'id');
}
Comment::with('commentable')->get();
Comment::with('commentable:id,name')->get();