从一个模型调用方法到另一个模型时,Laravel 5.4 BadMethodCallException错误

时间:2017-04-22 09:54:31

标签: php laravel model laravel-5.4

我正在使用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();
    }

我是否正确调用了该方法请帮助我

0 个答案:

没有答案