diff --git a/app/Http/Controllers/FoodController.php b/app/Http/Controllers/FoodController.php index 20c53d2..48bc560 100644 --- a/app/Http/Controllers/FoodController.php +++ b/app/Http/Controllers/FoodController.php @@ -65,6 +65,7 @@ class FoodController extends Controller ['value' => 'tsp', 'label' => 'tsp.'], ['value' => 'tbsp', 'label' => 'tbsp.'], ['value' => 'cup', 'label' => 'cup'], + ['value' => 'oz', 'label' => 'oz'], ])); } diff --git a/database/Factories/FoodFactory.php b/database/Factories/FoodFactory.php index a549802..6d7bb88 100644 --- a/database/Factories/FoodFactory.php +++ b/database/Factories/FoodFactory.php @@ -27,7 +27,7 @@ class FoodFactory extends Factory 'source' => $this->faker->optional()->url, 'notes' => $this->faker->optional(0.25)->realText(), 'serving_size' => $this->faker->numberBetween(1, 3), - 'serving_unit' => $this->faker->randomElement(['tsp', 'tbsp', 'cup']), + 'serving_unit' => $this->faker->randomElement(['tsp', 'tbsp', 'cup', 'oz']), 'serving_weight' => $this->faker->numberBetween(5, 500), 'calories' => $this->faker->randomFloat(1, 0, 100), 'fat' => $this->faker->randomFloat(1, 0, 10), diff --git a/database/Factories/IngredientAmountFactory.php b/database/Factories/IngredientAmountFactory.php index d8e1c8c..fb2ce6b 100644 --- a/database/Factories/IngredientAmountFactory.php +++ b/database/Factories/IngredientAmountFactory.php @@ -25,12 +25,10 @@ class IngredientAmountFactory extends Factory if ($this->faker->boolean(90)) { $ingredient_factory = Food::factory(); $ingredient_type = Food::class; - $ingredient_unit = Nutrients::units()->pluck('value')->random(1)->first(); } else { $ingredient_factory = Recipe::factory(); $ingredient_type = Recipe::class; - $ingredient_unit = 'serving'; } $amounts = [1/8, 1/4, 1/3, 1/2, 2/3, 3/4, 1, 1 + 1/4, 1 + 1/3, 1 + 1/2, 1 + 2/3, 1 + 3/4, 2, 2 + 1/2, 3]; @@ -38,7 +36,6 @@ class IngredientAmountFactory extends Factory 'ingredient_id' => $ingredient_factory, 'ingredient_type' => $ingredient_type, 'amount' => $this->faker->randomElement($amounts), - 'unit' => $ingredient_unit, 'detail' => $this->faker->boolean() ? Words::randomWords('a') : null, 'weight' => $this->faker->numberBetween(0, 50), 'parent_id' => Recipe::factory(), @@ -46,6 +43,18 @@ class IngredientAmountFactory extends Factory ]; } + public function configure(): static + { + return $this->afterMaking(function (IngredientAmount $ingredient_amount) { + // Set the unit to a random one supported by the ingredient. + $ingredient_amount->unit = $ingredient_amount->ingredient + ->units_supported + ->random(1) + ->pluck('value') + ->first(); + }); + } + /** * Define a specific parent. */