php - 在 laravel 5 的递归关系中包含回复

标签 php laravel-5

我有一个名为 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()

我不知道该怎么做。 什么是解决方案?

最佳答案

我的答案是:

laracasts.com/discuss

这是我必须添加的关系:

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/

相关文章:

php - Laravel sum() 集合上的方法返回表中所有项目的结果

php 日期到 mysql 返回 0

php - PHP 的 PDO 是否可以限制为单个查询?

php - CakePHP 如何在同一请求中更改 __() 语言

laravel-5 - 在 Homestead 上使用 Laravel 5.2 PHP7 phpStorm 10 的 xDebug 未启动

php - 使用 Dompdf 保存 PDF 文件

php - 为什么这些 UPDATE 和 INSERT INTO 查询不起作用? PHP 和 MySQL

php - 调用PHP函数error_message()时发生 fatal error

javascript - Laravel:compiled.php 第 7772 行中的 RuntimeException:发出 Angular http 请求时

php - 如何使用 Laravel 从外部 api 响应源下载文件?