Few days ago, I saw a website where users can comment on a post. Other users can reply on that. and a replay can have replies like the example screenshot below..
So
Swap reply_id
with a nullable()
parent_id
. So basically you want to know if a comment has a parent.
Then in Comment
model, add a self relationship to take all comments whose parent_id
match.
public function replies() {
return $this->hasMany('App\Comment', 'parent_id');
}
In your view you can have nested loops for each comments and its replies
@foreach($comments as $comment)
{{ $comment->content }}
@if ( $comment->replies )
@foreach($comment->replies as $rep1)
{{ $rep1->content }}
...
@endforeach
@endif
@endforeach