Get average review for Reviews model within model...?
Hi All,
I am trying to calculate the average rating in my product model however getting the following error. No idea what is going on as this seems like it should work...
Call to a member function addEagerConstraints() on float
Product Model
public function reviews()
{
return $this->hasMany('App\Review');
}
public function avgReviewRating()
{
return $this->reviews()->avg('rating');
}
Also, I am trying to avoid loading all data for every review in my view. I just want to do the calculation for each product and display the result.
Show where you're using it. avg() actually executes the query and returns a float value. It sounds like you're trying to use it within a relationship constraint, which won't work because it doesn't return a builder instance to chain onto.
Edit: you just posted your code and that's the issue. avgReviewRating is not a relationship, so you can't use it in a with.