我正在使用laravel 5.4我正在调用从我的评论模型到产品模型的一个方法,同时调用该方法会抛出错误
Builder.php第2445行中的BadMethodCallException:调用undefined 方法Illuminate \ Database \ Query \ Builder :: recalculateRating()
我的评论模型是
public function storeReviewForProduct($productID, $review, $rating)
{
$product = Product::find($productID);
$this->user_id = Auth::user()->id;
$this->review = $review;
$this->rating = $rating;
$product->reviews()->save($this);
// recalculate ratings for the specified product
$product->recalculateRating($rating);
}
我的产品型号是:
public function recalculateRating($rating)
{
$reviews = $this->reviews()->notSpam()->approved();
$avgRating = $reviews->avg('rating');
$this->rating_cache = round($avgRating,1);
$this->rating_count = $reviews->count();
$this->save();
}
我是否正确调用了该方法请帮助我