请问定义morphTo多态,在查询的时候 如果根据不同的模型来select不同的字段?
比如有个评论表comments定义了
public function commentable()
{
return $this->morphTo();
}
news表的字段是id, title
article表的字段是id, sub_title
在查询comments记录的时候 如何用with查询,判断如果是 news模型则select id,title, 如果是article模型则select id, sub_title?
with(['commentable' => function ($query) {
// 如果是news
$query->select(['id', 'title']);
// 否则如果是article
$query->select(['id', 'sub_title']);
}])
推荐文章: