【问题标题】:laravel stop nested reply form in loop [closed]laravel 在循环中停止嵌套回复表单[关闭]
【发布时间】: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

【问题讨论】:

    标签: laravel laravel-8 laravel-blade


    【解决方案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
    

    【讨论】:

    • 你可以使用-&gt;isNotEmpty(),因为它是一个集合
    • 每次回复后仍显示回复表单
    猜你喜欢
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多