我有一个名为 replies 的表,其中包含论坛 laravel 应用程序中某个主题的所有回复。
**replies Table**
---------------
reply_id
content
topic_id
reply_by
embed_reply
created_at
updated_at
回复可以有另一个回复 Herself。因为有 embed_reply 列包含包含回复的 reply_id。
现在我希望包含的回复的详细信息在获取时存在于父回复中。
为此我添加了这个方法来回复模型:
public function included_reply ()
{
return $this->with('replies', function ($query) {
$query->where('reply_id',$this->embed_reply);
});
}
为了获取特定主题的回复,我写了这个:
$topic = Topic::whereTopicId($id)
->with([
'replies' => function ($query) {
$query->orderBy('created_at', 'desc');
}
, 'replies.included_reply'
])
->first();
所有这些都返回以下错误:
BadMethodCallException in Builder.php line 2071:
Call to undefined method Illuminate\Database\Query\Builder::replies()
我不知道该怎么做。 什么是解决方案?
最佳答案
我的答案是:
这是我必须添加的关系:
public function embed()
{
return $this->belongsTo(Reply::class, 'embed_reply');
}
这是获取那个:
$reply = Reply::find($reply_id);
$embed = $reply->embed;
关于php - 在 laravel 5 的递归关系中包含回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33891187/