gpt4 book ai didi

php - 输出对评论的回复

转载 作者:行者123 更新时间:2023-12-05 02:37:49 25 4
gpt4 key购买 nike

我已经给文章添加了评论,这里是所有字段

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 个评论 block ,一个是常规评论,另一个是对它的回答。它们之间唯一的区别是类不同

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

普通评论

<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();

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

最佳答案

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

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

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

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

//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');
}

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

在下面的示例查询中,我们正在寻找 ID 为 1 的文章的评论(如果有的话,还有它的回复)。该文章有 2 条评论。第一条评论有 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": [

]
}
]
}
]

Note: I think the above method is more efficient if you want todisplay the article,comments and replies in the same page as it doesonly one query.

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

只是对评论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"
}
]
}
]

关于php - 输出对评论的回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69901159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com