0

I have a shop where people can leave a comment for a product. On the front page I would like to display the 2 best comments for each product. So I want to eager load the best 2 comments for each product when loading the products.

This is what I tried initially:

Product::with(['comments' => function ($query) {
  $query
  ->orderBy('points', 'desc')
  ->limit(2);
}])->get()

Instead of giving me the 2 best comments per product, it only gives me the 2 best comments of all the products.

Do you have any idea how to approach this? Thank you!

lagbox
  • 48,571
  • 8
  • 72
  • 83
Hillcow
  • 890
  • 3
  • 19
  • 48

0 Answers0