Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

480
Visualizações
Looping nested comments in Laravel 8.x

In Laravel 8.x I am trying to create a blog comment system which allows you to reply to comments. If replying to a comment, the comment is assigned a parent_id which is the id of the comment they are replying to. Currently when I loop the comments with the replies, it will only output a reply 1 loop deep like the example below:

Current example of the problem:

User1: Random post 1..
> User2: This text is a response to User1 for post 1
User6: Random post 2..
User7: Random post 3..

What I am trying to achieve:

User1: Random post 1..
> User2: This text is a response to User1 for post 1
>> User3: This text is a response to User2 for post 1
>>> User4: This text is a response to User3 for post 1
>>> User5: This text is a response to User3 for post 1
User6: Random post 2..
User7: Random post 3..

My current code

Model:
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

Now if I add @foreach ($comment->replies as $reply) 4 times within the comment loop, it will display the replies.. but of course this isn't practical since there can be unlimited responses to a comment. I hope you can understand what I'm trying to get at, I'm awfully bad at explaining things.

Any help is really appreciated :)

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

create two blade files

  1. comment-list.blade.php
  2. child-comment-list.blade.php

In comment-list.blade.php file

@if(count((array)$comments))
    @foreach ($comments as $comment)
    
        <p> {{ $comment->user->name }} : {{ $comment->comment }} </p>
    
       @include('child-comment-list',['comments'=>$comment->replies])
    
        @endforeach
@endif
    

In child-comment-list.blade.php file

@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

So in your current file

@include('comment-list',['comments'=>$comments]);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda