Hi, I am building an API where I have comments and single level replies i.e. a comment can have replies, but replies can't have any replies. My implementation has a single comments table where replies and comments are identified by parent_id column.
I am trying to fetch all comments in a post such that, first a parent comment is fetched, and their replies are fetched and when replies for the first comment are finished, second comment is fetched.
Similar thing can be easily done if I want to fetch all the comments and their replies without pagination but what I want to do is, apply pagination to comments and count comments and replies while applying the pagination limit. For example, my pagination limit is 8, if I have 5 comments and 10 replies in first comment, I will fetch first comment and 7 replies in that comment in the first page and leave other replies and comments to be fetched on next page request.
I am currently using this eloquent query to fetch comments:
$comments = Comment::where('post_id', 1)->simplePaginate(8);
The problem with this query is, it fetches all comments and replies on their order of insertion. For example:
{
"id": 1,
"user_id": 2,
"post_id": 1,
"parent_id": null,
"content": "Test Comment by me",
"created_at": "2022-05-04T10:55:03.000000Z",
"updated_at": "2022-05-04T10:55:03.000000Z",
},
{
"id": 2,
"user_id": 2,
"post_id": 1,
"parent_id": 1,
"content": "Test Comment by me",
"created_at": "2022-05-04T11:00:16.000000Z",
"updated_at": "2022-05-04T11:00:16.000000Z",
},
{
"id": 3,
"user_id": 2,
"post_id": 1,
"parent_id": null,
"content": "Test Comment by me",
"created_at": "2022-05-04T11:00:23.000000Z",
"updated_at": "2022-05-04T11:00:23.000000Z",
},
{
"id": 4,
"user_id": 2,
"post_id": 1,
"parent_id": null,
"content": "Test Comment by me",
"created_at": "2022-05-04T11:00:39.000000Z",
"updated_at": "2022-05-04T11:00:39.000000Z",
},
{
"id": 5,
"user_id": 2,
"post_id": 1,
"parent_id": 1,
"content": "Test Reply by me",
"created_at": "2022-05-04T11:07:44.000000Z",
"updated_at": "2022-05-04T11:07:44.000000Z",
},
{
"id": 6,
"user_id": 2,
"post_id": 1,
"parent_id": 2,
"content": "Test Comment by me",
"created_at": "2022-05-04T11:08:03.000000Z",
"updated_at": "2022-05-04T11:08:03.000000Z",
},
{
"id": 7,
"user_id": 2,
"post_id": 1,
"parent_id": 1,
"content": "Test Comment by me",
"created_at": "2022-05-04T11:17:01.000000Z",
"updated_at": "2022-05-04T11:17:01.000000Z",
},
{
"id": 8,
"user_id": 2,
"post_id": 1,
"parent_id": 1,
"content": "Test Reply by me",
"created_at": "2022-05-04T11:18:53.000000Z",
"updated_at": "2022-05-04T11:18:53.000000Z",
}
What I want is like this:
{
"id": 1,
"user_id": 2,
"post_id": 1,
"parent_id": null,
"content": "Test Comment by me",
"created_at": "2022-05-04T10:55:03.000000Z",
"updated_at": "2022-05-04T10:55:03.000000Z",
},
{
"id": 2,
"user_id": 2,
"post_id": 1,
"parent_id": 1,
"content": "Test Comment by me",
"created_at": "2022-05-04T11:00:16.000000Z",
"updated_at": "2022-05-04T11:00:16.000000Z",
},
{
"id": 5,
"user_id": 2,
"post_id": 1,
"parent_id": 1,
"content": "Test Reply by me",
"created_at": "2022-05-04T11:07:44.000000Z",
"updated_at": "2022-05-04T11:07:44.000000Z",
},
{
"id": 7,
"user_id": 2,
"post_id": 1,
"parent_id": 1,
"content": "Test Comment by me",
"created_at": "2022-05-04T11:17:01.000000Z",
"updated_at": "2022-05-04T11:17:01.000000Z",
},
{
"id": 8,
"user_id": 2,
"post_id": 1,
"parent_id": 1,
"content": "Test Reply by me",
"created_at": "2022-05-04T11:18:53.000000Z",
"updated_at": "2022-05-04T11:18:53.000000Z",
}
{
"id": 3,
"user_id": 2,
"post_id": 1,
"parent_id": null,
"content": "Test Comment by me",
"created_at": "2022-05-04T11:00:23.000000Z",
"updated_at": "2022-05-04T11:00:23.000000Z",
},
{
"id": 6,
"user_id": 2,
"post_id": 1,
"parent_id": 3,
"content": "Test Comment by me",
"created_at": "2022-05-04T11:08:03.000000Z",
"updated_at": "2022-05-04T11:08:03.000000Z",
},
{
"id": 4,
"user_id": 2,
"post_id": 1,
"parent_id": null,
"content": "Test Comment by me",
"created_at": "2022-05-04T11:00:39.000000Z",
"updated_at": "2022-05-04T11:00:39.000000Z",
},
How do I structure my query to get this?