nicholasnet's avatar

Have Forum layout just like Laracasts

I'm working on a forum project similar to Laracasts.com with posts and replies. I need to display nested replies with indentation (only one level deep). In your opinion what will be the most efficient approach:

  1. Using two tables: threads and replies (with replies having self-referential relationships)
threads             replies
------------        ------------
id                  id
content             thread_id
                    reply_id
                    content

or

  1. Using three tables: threads, replies, and responses
threads             replies            responses
------------        ------------       --------------
id                  id                 id
content             thread_id          reply_id
                    content            content

Which approach Laracasts uses for its Forum? Any hints or suggestions?

0 likes
6 replies
jdc1898's avatar

One option, no need for a second or third table. The replies simply need to reference the ID of the parent ID. When it’s null, you know it’s the OP.

‘’’

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

public function parent()
{
    return $this->belongsTo(Post::class, 'parent_id');
}

$posts = Post::whereNull('parent_id')->with('replies')->get();

‘’’

nicholasnet's avatar

@jdc1898 Yeah that's another way of doing it. I think Stackoverflow uses similar approach but with comments table as well. Thanks for sharing.

martinbean's avatar

@nicholasnet I would not create separate tables for “replies” and “responses”. They’re both the same entity (a “post” in a thread) and is just going to lead to redundancy and duplication as you’re going to have multiple tables, models, etc duplicating the same schema.

nicholasnet's avatar

@martinbean Yes I agree with you. But I am not sure how complicated pagination will be in that case. Since thread can have multiple replies (which needs its own pagination) and replies can have replies (for each may needs its own pagination).

nicholasnet's avatar

@jlrdw Thanks for the suggestion. I checked out both the new and old series, and while they cover threads and replies, they don't address the hierarchy like the one you see in the Laracasts forum.

Please or to participate in this conversation.