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 Error in check.length("fill") : 'gpar'成分'fill'的长度不能为零
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导