4 Comments

Nested comments in Laravel

Last week I added a nested comments feature to my blog. You can see it at the bottom of this post, where I received a few comments. In this post, I would like to tell you how I added this feature.

Note: This is not a step-by-step tutorial. Rather, I'll explain some things that I think are interesting.

Migrations

First of all, I added a parent_id column in my comments migration.

$table->integer('parent_id')->nullable();

If it is null, it is a root comment and else it is nested.

Leaving comments

Next, I updated my Livewire component to allow for replies, but only on root comments. When someone clicks "reply", a parent property at the component will be set to that comment and it shows a little message like "In reply to: Jeroen".

When a user hits "save", a little check is done to see if the comment really belongs to the current post and if it is a root comment, because I only want to allow comments to be nested one level deep.

Showing comments

When showing comments, all root comments (with an empty parent_id) will be listed, and for each comment, all children will be listed as well. These are exactly the same, except the children are a little bit indented at the left and don't have a "reply" button.

Notifications

When someone replies to a comment, everyone who also replied to that comment will get notified, but participants on other comments won't.

Before this, everyone would get notified. But that's sometimes weird if there are two discussions below one post.

Conclusion

Thanks for reading! I hope you learned something. At least it was a joy to write.

Share this article:

Subscribe to my newsletter

Continue reading:

How to unit test Eloquent relationships

Last week I added a nested comments feature to my blog. You can see it at the bottom of this post, where I received a few comments. In this post, I would like to tell...

The TALL stack explained

Last week I added a nested comments feature to my blog. You can see it at the bottom of this post, where I received a few comments. In this post, I would like to tell...

Eloquent polymorphic relationships explained (with examples)

Last week I added a nested comments feature to my blog. You can see it at the bottom of this post, where I received a few comments. In this post, I would like to tell...

Leave a comment

Comments (4)

    jules 's avatar
    jules Reply
    4 months ago

    How do you query the comments for a post? Does every comment still include the post ID? Some sort of nested recursive query? Queries in a loop?

      Jeroen van Rensen's avatar
      Jeroen van Rensen
      4 months ago

      Hi jules,

      Every comment has the post id, but when fetching comments I use this query:

      $post->comments()->whereNull('parent_id')->get();
      

      I hope that answers your question!

    test's avatar
    test Reply
    3 months ago

    testing the above

      Jeroen van Rensen's avatar
      Jeroen van Rensen
      3 months ago

      👍🏼