//relation in Product
public function reviews()
{
return $this->hasMany(Review::class);
}
//query
Product::withCount('reviews')->withAvg('reviews', 'rating')->get();
select "product".*, (select count(*) from "review" where "product"."id" = "review"."product_id") as "review_count", (select avg("review"."rating") from "review" where "product"."id" = "review"."product_id") as "review_avg_rating" from "product"
@iamarbabhussain you can try the way suggested by @s4muel and another way - join the table and add 2 aggregation columns in select for this join, and see which one will works faster in your way