First, you should probably eager load the comments, like so:
$posts = Post::with('comments')->where('category_id', '=', 5)->get();
This bit doesn't do anything really:
$posts->each(function($post) {
$post->comments;
});
And this bit shouldn't work at all if there are comments, cause you can't really print a collection.
{{ $post->comments }}
As for your question about ordering your comments, you could do something like this:
$posts = Post::with(['comments' => function($query)
{
$query->orderBy('commentTimestamp', 'desc');
}])->where('category_id', '=', 5)->get();
Again, comments are being eager loaded, but then we order them as well at the same time.
In fact, I am aware of the eager loading solution.
But I want to evaluate posts and comments separately for caching. This way, if $comments changes for a post, I will only query $comments of this post, not all $posts and their comments ($posts are up to date).
Depending on how many posts you have per page eager loading might still perform better than caching comments.
Eager loading does 2 queries.
The first time you do that while caching the comments you have 1 query to get the posts and then 1 query per post to get the comments. Not very scalable. Any further views, though, you'll then only do 1 query.
You could just cache posts including the comments and then invalidate the cache when a new comment has been added. This should work fine unless you have an extremely busy site where people comment a lot. Gives you 2 queries initially, then 0 afterwards.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community