首页 > 解决方案 > 如何根据 parent_id 在回复评论中显示评论正文

问题描述

实际上,我很困惑地询问我的问题的标题。

我有这样的表评论

Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('parent_id')->unsigned()->nullable();
        $table->foreign('parent_id')->references('id')->on('comments')->onDelete('cascade');
            $table->text('body');
            $table->integer('commentable_id')->unsigned();
            $table->foreign('commentable_id')->references('id')->on('posts')->onDelete('cascade');
            $table->string('commentable_type');
            $table->timestamps();
    });

以及我在评论模型中显示来自 parent_id 的回复的关系。

public function myreplyfrom()
{
    return $this->belongsTo(Comment::class,'parent_id')->select('body');
}

我像这样在刀片中显示身体评论

@foreach($mycomments as $comment)                        
    <p> Reply from Comment-> {{ $comment->myreplyfrom }} </p>    
@endforeach

我的评论表是这样的 我的评论表

我的评论仪表板是这样的 我的评论仪表板

我所做的是正确显示评论正文,但我想删除{"body"},所以只评论没有数组和字段标题。

我预期的结果 Reply from Comment-> sdsdsReply from Comment-> woww

如何解决我的问题?

标签: phplaravel

解决方案


您应该使用它来查看回复的评论正文

@foreach($mycomments as $comment)
<p> Reply from Comment-> {{ $comment->myreplyfrom->body }} </p>
@endforeach


推荐阅读