So here is the perfect ans of my own question -
Whenever we think of loading related data , the first things came into mind is by using laravel eloquent eager loading . like :
In my case i wants to load Post with it's Comments, so with eager loading we write something like this
$posts = Post::with('comments')->paginate(10); OR
$posts = $this->post->with('comments')->paginate(10); OR
In this case it's really hard to paginate comment and if we can do by nested query. it's really hard to make next page request on that comment.
but this was not the right way to do it. yeah this was first cam into mind.. so
Solution
so basically what i did, i just wanted to show my users initially single comment for that i fetched my post with single & most popular comment. like this:
end point is: /posts
$posts = $this->post->with(['comments'=> function(query){
$query->first(); //your popular comment or other logic
}])->paginate(10);
this was initial comment for my post.
then i created new function for handling my asynchronous fetching comments.
like @edoc said :
end point is: posts/postId/comments @kfirba said
public function getPostComments($postId)
{
$post= Post::where('id',$id)->firstOrFail();
$comments = Comment::where('post_id', $postId)->with('user')->paginate(15);
return view('post.show', compact('post', 'comments'));
}
So now you can call this function for your paginated comments.
Taught , it will be awesome to explain this for someone else,
Thanks to
@edoc
@kfirba &
@JeroenVanOort