Answer the question
In order to leave comments, you need to log in
How to do it correctly (laravel)?
Hello. The database contains tables products and reviews. There is a rating row in the reviews table (from 1 to 5). For each product, you need to display its rating. I do the following.
In the "Products" model
public function reviews()
{
return $this->hasMany(CourseReviews::class);
}
public function getAvgRating(){
return round($this->reviews->pluck('rating')->avg());
}
<span class="star ">{{$course->getAvgRating()}}</span>
Answer the question
In order to leave comments, you need to log in
Considering that adding a review with a rating is much less frequent than an impression, it is logical to make an additional column in the product table or in a separate 1 to 1 linked table with frequently changed data, in which to recalculate this value when adding a review.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question