在 Laravel 8.x 中循环嵌套评论
Looping nested comments in Laravel 8.x
在 Laravel 8.x 我正在尝试创建一个允许您回复评论的博客评论系统。如果回复评论,评论会被分配一个 parent_id
,这是他们正在回复的评论的 id
。目前,当我用回复循环评论时,它只会输出一个回复 1 循环深度,如下例所示:
当前问题示例:
用户 1:随机 post 1..
> 用户 2:此文本是对 post 1
用户 1 的回复
用户 6:随机 post 2..
用户 7:随机 post 3..
我想要达到的目标:
用户 1:随机 post 1..
> 用户 2:此文本是对 post 1
用户 1 的回复
>> User3:此文本是对 post 1
User2 的回复
>>> User4:此文本是对 post 1
的 User3 的回复
>>> User5:此文本是对 post 1
的 User3 的回复
用户 6:随机 post 2..
用户 7:随机 post 3..
我当前的代码
模型:
class PostComment extends Model
{
use HasFactory;
public function replies()
{
return $this->hasMany($this, 'parent_id');
}
}
Blade:
@foreach ($comments as $comment)
<p> {{ $comment->user->name }} : {{ $comment->comment }} </p>
@foreach ($comment->replies as $reply)
<p> {{ $reply->user->name }} : {{ $reply->comment }} </p>
@endforeach
@endforeach
现在,如果我在评论循环中添加 @foreach ($comment->replies as $reply)
4 次,它就会显示回复。但这当然不切实际,因为可以对评论进行无限次回复。我希望你能理解我的意思,我很不擅长解释事情。
非常感谢任何帮助:)
创建两个 blade 个文件
comment-list.blade.php
child-comment-list.blade.php
在comment-list.blade.php文件中
@if(count((array)$comments))
@foreach ($comments as $comment)
<p> {{ $comment->user->name }} : {{ $comment->comment }} </p>
@include('child-comment-list',['comments'=>$comment->replies])
@endforeach
@endif
在child-comment-list.blade.php文件中
@if(count((array)$comments))
@foreach($comments as $comment)
<p> {{ $comment->user->name }} : {{ $comment->comment }} </p>
@if(count((array)$comment->replies))
@include('child-comment-list',['comments'=>$comment->replies])
@endif
@endforeach
因此在您当前的文件中
@include('comment-list',['comments'=>$comments]);
在 Laravel 8.x 我正在尝试创建一个允许您回复评论的博客评论系统。如果回复评论,评论会被分配一个 parent_id
,这是他们正在回复的评论的 id
。目前,当我用回复循环评论时,它只会输出一个回复 1 循环深度,如下例所示:
当前问题示例:
用户 1:随机 post 1..> 用户 2:此文本是对 post 1
用户 1 的回复 用户 6:随机 post 2..
用户 7:随机 post 3..
我想要达到的目标:
用户 1:随机 post 1..> 用户 2:此文本是对 post 1
用户 1 的回复 >> User3:此文本是对 post 1
User2 的回复 >>> User4:此文本是对 post 1
的 User3 的回复 >>> User5:此文本是对 post 1
的 User3 的回复 用户 6:随机 post 2..
用户 7:随机 post 3..
我当前的代码
模型:class PostComment extends Model
{
use HasFactory;
public function replies()
{
return $this->hasMany($this, 'parent_id');
}
}
Blade:
@foreach ($comments as $comment)
<p> {{ $comment->user->name }} : {{ $comment->comment }} </p>
@foreach ($comment->replies as $reply)
<p> {{ $reply->user->name }} : {{ $reply->comment }} </p>
@endforeach
@endforeach
现在,如果我在评论循环中添加 @foreach ($comment->replies as $reply)
4 次,它就会显示回复。但这当然不切实际,因为可以对评论进行无限次回复。我希望你能理解我的意思,我很不擅长解释事情。
非常感谢任何帮助:)
创建两个 blade 个文件
comment-list.blade.php
child-comment-list.blade.php
在comment-list.blade.php文件中
@if(count((array)$comments))
@foreach ($comments as $comment)
<p> {{ $comment->user->name }} : {{ $comment->comment }} </p>
@include('child-comment-list',['comments'=>$comment->replies])
@endforeach
@endif
在child-comment-list.blade.php文件中
@if(count((array)$comments))
@foreach($comments as $comment)
<p> {{ $comment->user->name }} : {{ $comment->comment }} </p>
@if(count((array)$comment->replies))
@include('child-comment-list',['comments'=>$comment->replies])
@endif
@endforeach
因此在您当前的文件中
@include('comment-list',['comments'=>$comments]);