From cfa9e2c1ebd2c1a332b16ffc73f8fbe28583e29a Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sun, 31 Jan 2021 14:17:11 -0800 Subject: [PATCH] Ensure tags persist from old form data --- app/Http/Controllers/FoodController.php | 7 +++++++ app/Http/Controllers/RecipeController.php | 7 +++++++ resources/views/foods/edit.blade.php | 8 +++++--- resources/views/recipes/edit.blade.php | 2 +- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/FoodController.php b/app/Http/Controllers/FoodController.php index b55538f..fc9d4bb 100644 --- a/app/Http/Controllers/FoodController.php +++ b/app/Http/Controllers/FoodController.php @@ -53,8 +53,15 @@ class FoodController extends Controller */ public function edit(Food $food): View { + // Convert string tags (from old form data) to a Collection. + $food_tags = old('tags', $food->tags->pluck('name')); + if (is_string($food_tags)) { + $food_tags = new Collection(explode(',', $food_tags)); + } + return view('foods.edit') ->with('food', $food) + ->with('food_tags', $food_tags) ->with('nutrients', Nutrients::$all) ->with('serving_units', new Collection([ ['value' => 'tsp', 'label' => 'tsp.'], diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 37ec01b..4d3fa2d 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -125,8 +125,15 @@ class RecipeController extends Controller } } + // Convert string tags (from old form data) to a Collection. + $recipe_tags = old('tags', $recipe->tags->pluck('name')); + if (is_string($recipe_tags)) { + $recipe_tags = new Collection(explode(',', $recipe_tags)); + } + return view('recipes.edit') ->with('recipe', $recipe) + ->with('recipe_tags', $recipe_tags) ->with('ingredients', $ingredients) ->with('steps', $steps) ->with('ingredients_units', Nutrients::$units); diff --git a/resources/views/foods/edit.blade.php b/resources/views/foods/edit.blade.php index 90d86ee..61ea2ec 100644 --- a/resources/views/foods/edit.blade.php +++ b/resources/views/foods/edit.blade.php @@ -66,7 +66,8 @@ type="text" name="serving_size" size="10" - :value="old('serving_size', $old_value)"/> + :value="old('serving_size', $old_value)" + required/> @@ -91,7 +92,8 @@ step="any" name="serving_weight" size="10" - :value="old('serving_weight', $food->serving_weight)"/> + :value="old('serving_weight', $food->serving_weight)" + required/> @@ -113,7 +115,7 @@ - +
diff --git a/resources/views/recipes/edit.blade.php b/resources/views/recipes/edit.blade.php index 3acb360..5f5b646 100644 --- a/resources/views/recipes/edit.blade.php +++ b/resources/views/recipes/edit.blade.php @@ -60,7 +60,7 @@
- +