2 回答
TA贡献1993条经验 获得超5个赞
不那么优雅,但可以满足您的需求。该->relationLoaded(relationName)检查它的加载。
return [
'comments' => $this->when(
$this->relationLoaded('comments')
|| $this->relationLoaded('comments_by_tag'),
function () {
return CommentItem::collection(
$this->relationLoaded('comments')
? $this->comments
: $this->comments_by_tag
);
}
),
];
TA贡献1821条经验 获得超4个赞
通过简单地检查whenLoaded方法的值。
whenLoaded 如果没有加载的关系,方法返回 null。
if ($comments = $this->whenLoaded('comments')) {
return ['comments' => $comments]
} else if ($commentsByTag = $this->whenLoaded('comments_by_tag')) {
return ['comments' => $commentsByTag];
}
- 2 回答
- 0 关注
- 147 浏览
添加回答
举报