Answers for "calculate average in eager loading laravel"

PHP
0

calculate average in eager loading laravel

public function reviewRows()
{
    return $this->hasManyThrough('ReviewRow', 'Review');
}

public function avgRating()
{
    return $this->reviewRows()
      ->selectRaw('avg(rating) as aggregate, product_id')
      ->groupBy('product_id');
}

public function getAvgRatingAttribute()
{
    if ( ! array_key_exists('avgRating', $this->relations)) {
       $this->load('avgRating');
    }

    $relation = $this->getRelation('avgRating')->first();

    return ($relation) ? $relation->aggregate : null;
}
Posted by: Guest on April-01-2021

Code answers related to "calculate average in eager loading laravel"

Browse Popular Code Answers by Language