【问题标题】:Outputting a reply to a comment输出对评论的回复
【发布时间】:2021-12-22 08:05:19
【问题描述】:

我已经在文章中添加了 cmets,这里是所有字段

Schema::create('article_comments', function (Blueprint $table) {
   $table->bigIncrements('id');
   $table->unsignedBigInteger('article_id');
   $table->foreign('article_id')
         ->references('id')->on('articles')->onDelete('cascade');
   $table->string('name');
   $table->string('email');
   $table->text('text');
   $table->string('date');

   $table->unsignedBigInteger('comment_id')->nullable();
   $table->foreign('comment_id')
         ->references('id')->on('article_comments')->onDelete('set null');

   $table->timestamps();
});

我有 2 个 cmets 块,一个是常规的,另一个是它的答案。它们之间的唯一区别是不同的类

这就是我把它们带出来的方式

普通评论

<div class="comment-list">
    @foreach($article_comments as $article_comment)
    <div class="comment-list__item">
        <div class="item-card">
            <div class="item-card__header">
                <div class="item-card__title">
                    <div class="label">
                        {!! $article_comment->name !!}
                    </div>
                    <div class="data">
                        {!! date('d F Y', strtotime($article_comment->date)) !!}
                    </div>
                </div>
            </div>
            <div class="item-card__content">
                {!! $article_comment->text !!}
            </div>
        </div>
    </div>
    @endforeach
</div>

给他的回复

<div class="comment-sub-list">
    <div class="comment-sub-list__item">
        <div class="item-card">
            <div class="item-card__header">
                <div class="item-card__title">
                    <div class="label">
                        {!! $article_comment->name !!}
                    </div>
                    <div class="data">
                        {!! date('d F Y', strtotime($article_comment->date)) !!}
                    </div>
                </div>
            </div>
            <div class="item-card__content">
                {!! $article_comment->text !!}
            </div>
        </div>
    </div>
</div>

如果comment_id字段填写,那么这将是这个ID的评论的答案,但我无法实现它

我正在尝试对 comment_id 字段的存在进行类型检查并显示此评论

$articleCommentsReply = $article_comments->where('comment_id', $article_comment->comment_id)
->whereNotNull('comment_id')
->first();

但最后这条评论显示了两次,其中一个就是它的答案

【问题讨论】:

  • articleCommentsReply 中找到记录,在查询末尾添加-&gt;get()-&gt;first()$article_comments-&gt;where('comment_id', $article_comment-&gt;comment_id)-&gt;first();
  • @Miladpegah 是的,它可以工作,但我想出的无论如何都不正确,它会找到并显示 comment_id = null 所在的 cmets,但只有在有值的情况下才需要
  • 已添加答案。希望它能解决您遇到的问题。

标签: php html laravel


【解决方案1】:
$articleCommentsReply = $article_comments
    ->where('comment_id', $article_comment->comment_id)
    ->first();

您缺少查询的输出(在这种情况下为-&gt;first()

这将为您返回一个集合,然后您应该能够像您尝试的那样访问您的财产。

还有一个基于您的评论的更新,因为您获得了可为空的comment_ids,您还可以通过将查询扩展到:

$articleCommentsReply = $article_comments
    ->where('comment_id', $article_comment->comment_id)
    ->whereNotNull('comment_id')
    ->first();

【讨论】:

  • 是的,它可以工作,但我想出的无论如何都不正确,它会找到并显示其中comment_id = null的cmets,但只有在有值的地方才有必要
  • @chu 我根据您的评论更新了我的答案。
  • 有效,但是现在您仍然需要以某种方式执行此操作,以便它是对具有当前 comment_id 的评论的回复,现在它只创建了 2 次相同的评论,其中一个是回答
  • 很抱歉,我想在这里帮助你,但你开始听起来有点粗鲁,没有什么特别的原因......“你不知道如何解决它?”那是什么意思?同样在您的代码中,没有用于创建评论的查询,该查询仅检索评论。
  • 对不起,我不是想对你无礼,我只是想问问你对这件事有什么想法
【解决方案2】:

根据我对您的问题的理解,您可以做些什么来实现它。

这里我假设文章可能有很多 cmets,每个评论可能有零个或多个答案,最多一级递归。

但是您可以将answers 关系更改为answer,如果您愿意,可以通过将其定义为hasOne 关系来代替使用get(),也可以使用first()。这种hasMany 方法的优势在于,如果您保持这种方式,它将适用于单个和多个相关模型。

您可以将answers 递归关系添加到ArticleComment 模型

//in ArticleComment.php
public function answers()
{
    return $this->hasMany(ArticleComment::class, 'comment_id', 'id');
}

我假设您已经定义了此关系,如果没有将此 comments 关系添加到 Article 模型。

//in Article.php
public function comments()
{
    return $this->hasMany(ArticleComment::class, 'article_id', 'id');
}

现在您可以使用以下查询来获取带有comments 的文章。如果该评论的答案存在,它将在answers 关系中返回。如果只是评论,它将只返回 comments 关系中的评论,answers 将是一个空数组。

在下面的示例查询中,我们正在寻找 id 为 1 的文章的 cmets(如果有则回复)。所述文章有 2 个 cmets。第一条评论有 3 个答案,第二条评论没有。

$query = Article::where('id', 1)->with(['comments' => function($q) {
            $q->whereNull('comment_id')->with('answers');
        }]);

$output = $query->get()->toArray();

这是该查询的输出结果。

[
  {
    "id": 1,
    "text": "article 1"
    "comments": [
      {
        "id": 1,
        "article_id": 1,
        "name": "a1q1",
        "email": "***",
        "text": "Article 1 Comment 1",
        "date": "2021-11-22 16:52:11",
        "comment_id": null
        "answers": [
          {
            "id": 5,
            "article_id": 1,
            "name": "a1q1a1",
            "email": "***",
            "text": "Answer 1 to Article 1 Comment 1",
            "date": "2021-11-22 16:52:11",
            "comment_id": 1
          },
          {
            "id": 6,
            "article_id": 1,
            "name": "a1q1a1",
            "email": "***",
            "text": "Answer 2 to Article 1 Comment 1",
            "date": "2021-11-22 16:52:11",
            "comment_id": 1
          }
        ]
      },
      {
        "id": 2,
        "article_id": 1,
        "name": "a1q2",
        "email": "***",
        "text": "Article 1 Comment 2",
        "date": "2021-11-22 16:52:11",
        "comment_id": null,
        "answers": [
          
        ]
      }
    ]
  }
]

注意:如果你想,我认为上述方法更有效 在同一页面中显示文章、cmets 和回复 只有一个查询。

如果您正在寻找的答案是如何单独检查和获取特定的 cmets,您可以使用以下查询。

只是对评论 id 1 的回复

$repliesToCommentOnly = ArticleComment::where('comment_id', $articleComment->id)
    ->get()->toArray();

输出

[
  {
    "id": 5,
    "article_id": 1,
    "name": "a1q1a1",
    "email": "***",
    "text": "Answer 1 to Article 1 Comment 1",
    "date": "2021-11-22 16:52:11",
    "comment_id": 1,
    "created_at": "2021-11-22T16:52:11.000000Z",
    "updated_at": "2021-11-22T16:52:11.000000Z"
  },
  {
    "id": 6,
    "article_id": 1,
    "name": "a1q1a1",
    "email": "***",
    "text": "Answer 2 to Article 1 Comment 1",
    "date": "2021-11-22 16:52:11",
    "comment_id": 1,
    "created_at": "2021-11-22T16:52:11.000000Z",
    "updated_at": "2021-11-22T16:52:11.000000Z"
  }
]

文章评论及其回复

$commentWitItsReplies = ArticleComment::where('id', 1)->with('answers')
    ->get()->toArray();

输出

[
  {
    "id": 1,
    "article_id": 1,
    "name": "a1q1",
    "email": "***",
    "text": "Article 1 Comment 1",
    "date": "2021-11-22 16:52:11",
    "comment_id": null,
    "created_at": "2021-11-22T16:52:11.000000Z",
    "updated_at": "2021-11-22T16:52:11.000000Z",
    "answers": [
      {
        "id": 5,
        "article_id": 1,
        "name": "a1q1a1",
        "email": "***",
        "text": "Answer 1 to Article 1 Comment 1",
        "date": "2021-11-22 16:52:11",
        "comment_id": 1,
        "created_at": "2021-11-22T16:52:11.000000Z",
        "updated_at": "2021-11-22T16:52:11.000000Z"
      },
      {
        "id": 6,
        "article_id": 1,
        "name": "a1q1a1",
        "email": "***",
        "text": "Answer 2 to Article 1 Comment 1",
        "date": "2021-11-22 16:52:11",
        "comment_id": 1,
        "created_at": "2021-11-22T16:52:11.000000Z",
        "updated_at": "2021-11-22T16:52:11.000000Z"
      }
    ]
  }
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多