Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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).

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mehmet mehmet Joined 18 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.