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:
Using two tables: threads and replies (with replies having self-referential relationships)
threads replies
------------ ------------
id id
content thread_id
reply_id
content
or
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?
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 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.
@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).
@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.