我想基于相似的commentable_id(帖子)创建一组评论并显示它们。
我的问题是我无法显示所有回复评论。我只是显示评论的根,而不是回复。
我的控制器显示评论
public function index()
{
$mycomments = Comment::where('user_id', '=', Auth::user('member')->id)
->groupBy('commentable_id')
->with('replies')
->orderBy('created_at', 'desc')
->get();
return view('frontendblog.memberlogin.home', compact('mycomments'));
}
和我的评论模型
public function user()
{
return $this->belongsTo(Member::class);
}
public function post()
{
return $this->belongsTo(Post::class);
}
public function myreplyfrom()
{
return $this->belongsTo(Comment::class,'parent_id');
}
public function commentable()
{
return $this->morphTo();
}
public function replies()
{
return $this->hasMany(Comment::class, 'parent_id');
}
我的查看代码
@foreach($mycomments as $comment)
<a href="{{ route('blog.show', $comment->commentable->slug) }}#comments"> <p>Title Post->{{ $comment->commentable->title }}</p> </a>
<p> Author of Post-> {{ $comment->commentable->author->name }} </p>
<p>Comment->{{ $comment->body }}</p>
@if($comment->myreplyfrom)
<p> Reply from Comment-> {{ $comment->myreplyfrom->body }} </p>
@endif
{!! Form::open([
'method' => 'POST',
'route' => ['comment.destroy',"id_comment" => $comment->id, "id_member" => $comment->user_id]])
!!}
{!! Form::submit('delete', [
'onclick' => 'deleteConfirm()',
'id' => 'from1',
])!!}
{!! Form::close() !!}
<hr/>
@endforeach
我的结果 像这样
标题->准拟人化的拟南芥和拟人化的准个人。
Post-> Ilham Firman A的作者
评论->动作1(删除)
Title Post-> Indicunt Provident aut atque qui。
Post-> Ilham Firman A的作者
评论->哇(删除)
预期结果
职称->拟任职者阿尼米·马格南·Incidunt qui Debitis et nesciunt承担假设。
Post-> Ilham Firman A的作者
评论->动作1(删除)
评论->回复动作1(删除)
Title Post-> Indicunt Provident aut atque qui。
Post-> Ilham Firman A的作者
评论->哇(删除)
评论-> sjksjks(删除)
评论-> sdsds(删除)
答案 0 :(得分:1)
问题是这一行:
->groupBy('commentable_id')
当您按commentable_id分组时,评论表中的每个帖子将只接受一个评论。
您可以执行以下操作:
// This is a collection of comments grouped by post id, so I rename the variable to $commentsGroupedByPost.
$commentsGroupedByPost = Comment::where('user_id', '=', Auth::user('member')->id)
->with('replies', 'commentable')
->orderBy('created_at', 'desc')
->get()
->groupBy('commentable_id');
然后从视图中查看
:@foreach($commentsGroupedByPost as $postId => $comments)
<a href="{{ route('blog.show', $comments->first()->commentable->slug) }}#comments">
<p>Title Post->{{ $comments->first()->commentable->title }}</p>
</a>
<p> Author of Post-> {{ $comments->first()->commentable->author->name }} </p>
@foreach($comments as $comment)
<p>Comment->{{ $comment->body }}</p>
@foreach($comment->replies as $reply)
<p> Reply from Comment-> {{ $reply->body }} </p>
@endforeach
{!! Form::open([
'method' => 'POST',
'route' => ['comment.destroy',"id_comment" => $comment->id,
"id_member" => $comment->user_id]])
!!}
{!! Form::submit('delete', [
'onclick' => 'deleteConfirm()',
'id' => 'from1',
])!!}
{!! Form::close() !!}
@endforeach
<hr/>
@endforeach
答案 1 :(得分:0)
我不确定您要达到的目标是100%,但是我认为您只是想获得第一条评论/答复?
如果是这样,请尝试以下操作:
$mycomments = Comment::where('user_id', '=', Auth::user('member')->id)
->groupBy('commentable_id')
->with(['replies', function($query) {
$query->latestFirst();
}])
->get();
我不确定您的lastFirst()方法在做什么,但是我添加的闭包应该为您提供回复/评论的第一个实例。