Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

481
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda