Php 如何根据父项id在回复注释中显示注释正文
事实上,我想问一下问题的题目 我有这样的意见表Php 如何根据父项id在回复注释中显示注释正文,php,laravel,Php,Laravel,事实上,我想问一下问题的题目 我有这样的意见表 Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); $table->integer('parent_id')->unsigned()->nullable();
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();
});
@foreach($mycomments as $comment)
<p> Reply from Comment-> {{ $comment->myreplyfrom }} </p>
@endforeach
我的关系是在评论模型中显示家长id的回复,如下所示
public function myreplyfrom()
{
return $this->belongsTo(Comment::class,'parent_id')->select('body');
}
我在刀片上显示身体评论,如下所示
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();
});
@foreach($mycomments as $comment)
<p> Reply from Comment-> {{ $comment->myreplyfrom }} </p>
@endforeach
@foreach($mycomments作为$comment)
来自评论的回复->{{$Comment->myreplyfrom}
@endforeach
我的评论表是这样的
我的评论面板是这样的
我所做的是正确地显示注释主体,但我想删除{“body”}
,所以只需不带数组和字段标题的注释即可
我期望的结果是
评论回复->sdsds
和评论回复->woww
如何解决我的问题???您应该在回复的评论正文中使用此选项
@foreach($mycomments as$comment)
来自评论的回复->{{$Comment->myreplyfrom->body}
@endforeach
您应该在回复的注释正文视图中使用此选项
@foreach($mycomments as$comment)
来自评论的回复->{{$Comment->myreplyfrom->body}
@endforeach
我试图获取非对象的属性时出错。您可以看到关系{{$comment->myreplyfrom?$comment->myreplyfrom->body:'NO REPLY'}}
这样使用。。。。由于注释可能有父id,也可能没有父id,这是因为id为1的注释没有父id,所以使用上述注释中提到的三元运算符是的,这是有效的。这是我的错。谢谢你的解释。我必须检查是否没有父id。尝试获取非对象的属性时出现错误。您可以看到关系{{$comment->myreplyfrom?$comment->myreplyfrom->body:'NO REPLY'}}
这样使用。。。。由于注释可能有父id,也可能没有父id,这是因为id为1的注释没有父id,所以使用上述注释中提到的三元运算符是的,这是有效的。这是我的错。谢谢你的解释。我必须检查是否没有家长id。