@unixlab but then this one does not show how do you apply a pagination. If you applied a pagination on the grouped results, then for sure you will get just two results back.
How is your relationship setup between a Comment and a Child comment. Share more code please, from the view, the model..
@unixlab I guess I will have to ask for each code block separately.. if you re-read my reply above I asked for much more than just the pagination. I don't know how is your relationship setup between a Comment and a Child comment.. How do you differentiate a comment from a child comment?
With what you have shown tells me that you just want to take two results from the comments, not including any child comments, and in your case the comments and child comments are at the same level, which means you might display them using some conditions or a separate query in the view..
So either you share all the code that is involved for displaying the comments, or you find your way around it alone taken what I am saying here :)
@unixlab okay so there is a parent_id that's how the child is connected to a parent.. His way is a bit different, he structures the tree on an instance of the collection, that's why you get two results only..
I would do the following, in the Comment model, add this for example:
public function replies() {
return $this->hasMany(Comment::class,'parent_id','id') ;
}
@unixlab nope. with() is used to eager load the relationship. Which means reduced number of queries. Unless you use with() then yes, calling $comment->replies will perform another query
And one more thing if you don't mind.
Now works perfectly except the fact child comments will not have any pagination at all. So in the feature if i want to paginate the subcomments what are my options ?
@unixlab yes, but after using with() calling $comment->replies uses the eager loaded collection won't do an extra query no worries here.
To me it does not make sense to have a pagination on the replies, you might need a JavaScript for this for example, to show the latest two replies and then a Load more button, but that will all be scripted I guess, instead of using the Query Builder in this case only as it will look odd having a pagination links for both comments and replies.