Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Marcolino922's avatar

Advice on how to manage the comment list and replies to comments

Hi, in my little application I would like to create a comment and replies to comments system. Before we started, I wanted some advice on how best to structure everything.

Is it advisable to create two different tables in the database? Example: comments, replies_to_comments and obviously the two related models.

Furthermore, by choosing this option, as far as the administrative side is concerned, how can I manage the comments in a single list which therefore only results in comments and responses to comments? (edit and delete).

I accept explanations in this regard, Thanks to anyone who will want to advise me

0 likes
7 replies
gitwithravish's avatar

Option 1

  • comments and replies table

https://laracasts.com/discuss/channels/eloquent/how-to-get-posts-with-comments-and-replies

Option 2

  • comments table to store both
  • This one might feel a little tricky but it is a good design. You really dont need to make two tables. Reply can be considered as comment on another comment.
  • A column parent_id can be made to link parent comment. A column with parent_id null means its a comment, and any comment with integer parent_id means its a reply to comment if defined in the given column.
  • In this case you can also achieve nested comments/replies.
  • Note that in the following link there are two columns commentable_type, and commentable_id. It is used when your application has comments on multiple things like posts, videos etc. In that case this polymorphic approach is useful. But since your application is "little" you might not need these. You can instead have only one column post_id

https://www.codecheef.org/article/create-your-own-multilevel-nested-comments-system-in-laravel

Hope this helps @marcolino922

Marcolino922's avatar

Hi, I'm not finding the right logic to create the comment system like in the second example.

I created a table in the database:

screenshot-localhost-2021-05-06-10-14-22

Next, the controller and models:

Controller

'comments' => $item->comments()->orderByDesc('id')->paginate(15),

Items

public function comments()
{
      return $this->morphMany(Comments::class, 'commentable');
}

Comments

public function replies()
    {
        return $this->hasMany(Comments::class, 'parent_id');
    }

Unfortunately, however, I do not understand how to manage the answers, in the sense, showing the list of comments within a foreach loop, how to uncouple the answers by associating them with the comment they belong to?

Marcolino922's avatar

I can't understand how to manage the layout part to nest the various comments, since they are all under the same cycle, how can I move the answers under the comment for example with a margin?

@foreach($comments as $comment)
{{ $comment->body }}
------ Form reply
@endforeach
Marcolino922's avatar

By adding "whereNull ()" it fetches only the comments, without the answers. In replies, the "parent_id" field is filled with the id of the comment to which the reply belongs.

public function comments()
    {
        return $this->morphMany(Comments::class, 'commentable')->whereNull('parent_id');
    }

I'm missing something

gitwithravish's avatar

Well, right now there is only one partial blade file that handles comments and replies. If you want to give different css to comments and replies. There are two ways to go.

Option 1

Write different code for comment and reply under the same file by putting conditions. Now you apply different css to replies and different css to main comment. replies.blade.php

@foreach($comments as $comment)
	@if(is_null($comment->parent_id))
		// write html-css for comment
	@else
		// write html-css for reply
	@endif
@endforeach 

Option 2

If the code is getting messy then create two partial files comments.blade.php and replies.blade.php. Call the second partial from the first one if the comment has replies.

@marcolino922

Please or to participate in this conversation.