Home > database >  laravel stop nested reply form in loop
laravel stop nested reply form in loop

Time:10-02

how can i stop the reply form from showing and repeating its self after a reply has been made

@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

CodePudding user response:

You should have be able to figure it out. It's not that tricky

@if (!empty($comment->replies))
    @include('posts.comments.reply', ['comments' => $comment->replies])
@empty

CodePudding user response:

    @forelse($comments as $comment)
     <div class="display-comment">
       <strong>{{ $comment->user->name }}</strong>
     <p>{{ $comment->comment }}</p>
     <a href="" id="reply"></a>
    
     @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>
  • Related