donglvhe7591 2015-11-24 10:28
浏览 86
已采纳

在laravel 5中以递归关系包含回复

I have a table named replies contains all replies of a topic in a Forum laravel application.

**replies Table**
---------------
    reply_id
    content
    topic_id
    reply_by
    embed_reply
    created_at
    updated_at

replies can have another reply Herself.for that there is embed_reply column that hold reply_id of included reply.

Now I want details of included reply would have existed on the parent Reply on fetching.

for that i add this Method to reply Model :

public function included_reply ()
        {
            return $this->with('replies', function ($query) {
                $query->where('reply_id',$this->embed_reply);
            });
        }

And for fetching replies of a specific Topic, I wrote this :

$topic = Topic::whereTopicId($id)
                ->with([
                    'replies' => function ($query) {
                        $query->orderBy('created_at', 'desc');
                    }
                    , 'replies.included_reply'
                ])
                ->first();

all of this return bellow Error:

BadMethodCallException in Builder.php line 2071:
Call to undefined method Illuminate\Database\Query\Builder::replies()

and I do not know how to do that. what is solution?

  • 写回答

1条回答 默认 最新

  • dongtuo4132 2015-11-24 15:17
    关注

    I got my answer in:

    laracasts.com/discuss

    This is relationship that I must be added:

    public function embed()
    {
        return $this->belongsTo(Reply::class, 'embed_reply');
    }
    

    and this for fetching that :

    $reply = Reply::find($reply_id);
    $embed = $reply->embed;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址