hasMany(IngredientAmount::class); } /** * Get total recipe calories. */ public function calories(): float { return $this->sumNutrient('calories'); } /** * Get total recipe protein. */ public function protein(): float { return $this->sumNutrient('protein'); } /** * Get total recipe fat. */ public function fat(): float { return $this->sumNutrient('fat'); } /** * Get total recipe carbohydrates. */ public function carbohydrates(): float { return $this->sumNutrient('carbohydrates'); } /** * Sum a specific nutrient for all ingredient amounts. * * @param string $nutrient * Nutrient to sum ("calories", "protein", "fat", or "carbohydrates"). * * @return float */ private function sumNutrient(string $nutrient): float { $sum = 0; foreach ($this->ingredientAmounts as $ingredientAmount) { $sum += $ingredientAmount->{$nutrient}(); } return $sum; } }