John Davidson

php - Outputting a reply to a comment

0 comments
Message:


I have add comments to the article, here are all the fields


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

I have 2 blocks of comments, one regular and the second answer to it. The only difference between them is the different classes


This is how I bring them out


Normal comment


<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>

The reply to him


<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>

If the comment_id field is filled in, then this will be the answer to the comment with this ID, but I can't implement it


I'm trying to type-check for the presence of the comment_id field and display this comment


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

But in the end, this comment is displayed twice, and one of them is the answer to it



My Answer:
>increments('id');
$table->integer('article_id');
$table->string('commenter_name');
$table->text('comment');
$table->timestamps();
});_

To output a reply to a comment in PHP, you can use the following code snippet:

php
// Assuming $comment is the comment object with fields id, article_id, commenter_name, comment
// $reply is the reply to the comment

echo "Replying to comment by " . $comment->commenter_name . ": " . $reply;


This code will output a message indicating that you are replying to a specific comment by a commenter and display the reply text. You can customize the output message as needed.

Rate this post

3 of 5 based on 5817 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us