laravel 在循环中停止嵌套回复表单[关闭]
Posted
技术标签:
【中文标题】laravel 在循环中停止嵌套回复表单[关闭]【英文标题】:laravel stop nested reply form in loop [closed] 【发布时间】:2021-11-22 23:56:14 【问题描述】:如何在回复完成后阻止回复表单显示和重复其自身
@foreach($comments as $comment)
<div class="display-comment">
<strong> $comment->user->name </strong>
<p> $comment->comment </p>
<a href="" id="reply"></a>
<form method="post" action=" route('reply.add') ">
@csrf
<div class="form-group">
<input type="text" name="comment" class="form-control" />
<input type="hidden" name="post_id" value=" $post_id " />
<input type="hidden" name="comment_id" value=" $comment->id " />
</div>
<div class="form-group">
<input type="submit" class="py-0 btn btn-sm btn-outline-danger" style="font-size: 0.8em;" value="Reply" />
</div>
</form>
@include('posts.comments.reply', ['comments' => $comment->replies])
</div>
@endforeach
【问题讨论】:
【参考方案1】: @forelse($comments as $comment)
<div class="display-comment">
<strong> $comment->user->name </strong>
<p> $comment->comment </p>
@include('posts.comments.reply', ['comments' => $comment->replies])
</div>
@empty
<div> No comments found</div>
@endforelse
<form method="post" action=" route('reply.add') ">
@csrf
<div class="form-group">
<input type="text" name="comment" class="form-control" />
<input type="hidden" name="post_id" value=" $post_id " />
<input type="hidden" name="comment_id" value=" $comment->id " />
</div>
<div class="form-group">
<input type="submit" class="py-0 btn btn-sm btn-outline-danger" style="font-size: 0.8em;" value="Reply" />
</div>
</form>
【讨论】:
回复表单仍在循环中自我重复 我已经编辑了我的代码,请重新考虑尝试一下【参考方案2】:您应该能够弄清楚。没那么难
@if (!empty($comment->replies))
@include('posts.comments.reply', ['comments' => $comment->replies])
@empty
【讨论】:
你可以使用->isNotEmpty()
,因为它是一个集合
每次回复后仍显示回复表单以上是关于laravel 在循环中停止嵌套回复表单[关闭]的主要内容,如果未能解决你的问题,请参考以下文章