'int', ]; /** * @inheritdoc */ protected array $with = ['steps', 'ingredientAmounts']; /** * Get the steps for this Recipe. */ public function steps(): HasMany { return $this->hasMany(RecipeStep::class)->orderBy('number'); } /** * Get the Ingredient Amounts used for this Recipe. */ public function ingredientAmounts(): HasMany { return $this->hasMany(IngredientAmount::class)->orderBy('weight'); } /** * Get total recipe calories. */ public function caloriesTotal(): float { return $this->sumNutrient('calories'); } /** * Get total recipe protein. */ public function proteinTotal(): float { return $this->sumNutrient('protein'); } /** * Get total recipe fat. */ public function fatTotal(): float { return $this->sumNutrient('fat'); } /** * Get total recipe carbohydrates. */ public function carbohydratesTotal(): 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; } }