morphMany(IngredientAmount::class, 'parent') ->with('ingredient') ->orderBy('weight'); } /** * Sum a specific nutrient for all ingredients. * * @param string $nutrient * Nutrient to sum. * * @return float */ private function sumNutrient(string $nutrient): float { $sum = 0; foreach ($this->ingredientAmounts as $ingredientAmount) { $sum += $ingredientAmount->{$nutrient}(); } return $sum; } }