Skip to content
Advertisement

Advices to implement child comments on comments (How to set it up)

I am working with Laravel and have created a morphable comment table with laravel. Now I want to implemnt the reply function so people can answer to comments. I only do need the first level of answering. So, you can answer a comment but you cannot answer on a comment child. There is only one level of comment childs and not 2 or 3 like on Facebook.

My question to you now is, which is the best way to solve this. Because in my laravel view blade I am looping through each comment and want to print my child comments on this commment to (I just don’t know how do to his?).

So basically, I would like to know how you would design the table, how you would map it and how you would print it in the view.

Here is my table so far:

Advertisement

Answer

Child comments should just be more comments except they are mapped to another comment.

Create a comment_children table.

Structure should be:

id: BIGINT comment_id: BIGINT child_id: BIGINT

Then you can create a hasManyThrough children relationship on your Comment model and use the comment_children table to match them.

This will allow you to eager load the child comments, you can make your mode always do this by adding:

You will also want a comments relationship on your PostStatus model.

Set up your model to always load this relationship also by adding:

Now whenever you fetch a post, the collection will always contain all the comments and all the children.

Now to access it, something like:

And in your blade:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement