php - 如何根据 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-> sdsds
是Reply from Comment-> woww
如何解决我的问题?
解决方案
您应该使用它来查看回复的评论正文
@foreach($mycomments as $comment)
<p> Reply from Comment-> {{ $comment->myreplyfrom->body }} </p>
@endforeach
推荐阅读
- jquery - Laravel - 使用现有行数据创建新用户
- c# - ASP .NET 中的全局异常过滤器
- ios - 弹出框关闭后,如何在 PageViewController 上显示警报?
- javascript - 如何将 css 样式从文件注入到 WebComponent
- flutter - 我使用了很多容器小部件。我做得对吗?
- amazon-web-services - IAM CodeBuild 策略条件 - 委托人标签等于资源标签
- javascript - parentElement 为空错误,找不到移动元素的方法
- python - decodedObjects = pyzbar.decode(im) -> 导致 -> Type Error: cannot unpack non-iterable NoneType object
- ios - 用 " 编码 XML 导致 XML 文档结构问题
- r - 使用 r-mice 时池函数导致估算数据错误