1public function reviewRows()
2{
3 return $this->hasManyThrough('ReviewRow', 'Review');
4}
5
6public function avgRating()
7{
8 return $this->reviewRows()
9 ->selectRaw('avg(rating) as aggregate, product_id')
10 ->groupBy('product_id');
11}
12
13public function getAvgRatingAttribute()
14{
15 if ( ! array_key_exists('avgRating', $this->relations)) {
16 $this->load('avgRating');
17 }
18
19 $relation = $this->getRelation('avgRating')->first();
20
21 return ($relation) ? $relation->aggregate : null;
22}
23