From dd40582b64ddd464789d28379267eee1a32790ac Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Thu, 31 Dec 2020 15:30:56 -0800 Subject: [PATCH] Update food views for servings data --- app/Http/Controllers/FoodController.php | 30 +++--- app/Models/JournalEntry.php | 3 + resources/views/foods/create.blade.php | 120 ++++++++++++++---------- resources/views/foods/index.blade.php | 21 +++-- 4 files changed, 102 insertions(+), 72 deletions(-) diff --git a/app/Http/Controllers/FoodController.php b/app/Http/Controllers/FoodController.php index 25db787..b961d11 100644 --- a/app/Http/Controllers/FoodController.php +++ b/app/Http/Controllers/FoodController.php @@ -6,6 +6,7 @@ use App\Models\Food; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Collection; class FoodController extends Controller { @@ -27,7 +28,12 @@ class FoodController extends Controller */ public function create(): View { - return view('foods.create'); + return view('foods.create') + ->with('serving_units', new Collection([ + ['value' => 'tsp', 'label' => 'tsp.'], + ['value' => 'tbsp', 'label' => 'tbsp.'], + ['value' => 'cup', 'label' => 'cup'], + ])); } /**newly @@ -36,16 +42,18 @@ class FoodController extends Controller public function store(Request $request): RedirectResponse { $attributes = $request->validate([ - 'name' => 'required|string', - 'detail' => 'nullable|string', - 'carbohydrates' => 'nullable|numeric', - 'calories' => 'nullable|numeric', - 'cholesterol' => 'nullable|numeric', - 'fat' => 'nullable|numeric', - 'protein' => 'nullable|numeric', - 'sodium' => 'nullable|numeric', - 'unit_weight' => 'required_without:cup_weight|nullable|numeric', - 'cup_weight' => 'required_without:unit_weight|nullable|numeric', + 'name' => 'required|string', + 'detail' => 'nullable|string', + 'brand' => 'nullable|string', + 'serving_size' => 'required|numeric', + 'serving_unit' => 'nullable|string', + 'serving_weight' => 'required|numeric', + 'calories' => 'nullable|numeric', + 'fat' => 'nullable|numeric', + 'cholesterol' => 'nullable|numeric', + 'sodium' => 'nullable|numeric', + 'carbohydrates' => 'nullable|numeric', + 'protein' => 'nullable|numeric', ]); /** @var \App\Models\Food $food */ $food = tap(new Food(array_filter($attributes)))->save(); diff --git a/app/Models/JournalEntry.php b/app/Models/JournalEntry.php index 7c06194..acf767d 100644 --- a/app/Models/JournalEntry.php +++ b/app/Models/JournalEntry.php @@ -6,6 +6,9 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +/** + * TODO: Change this model to track nutrients _directly_. + */ class JournalEntry extends Model { use HasFactory; diff --git a/resources/views/foods/create.blade.php b/resources/views/foods/create.blade.php index 8f5dfe8..ed16926 100644 --- a/resources/views/foods/create.blade.php +++ b/resources/views/foods/create.blade.php @@ -28,7 +28,7 @@
@csrf
-
+
@@ -51,12 +51,62 @@ name="detail" :value="old('detail')"/>
+ + +
+ + + +
+
+ +
+ +
+ + + +
+ + +
+ + + + + +
+ + +
+ + + +
- +
- -
- - - -
- - -
- - - -
-
- +
- +
- + - + :value="old('cholesterol')"/>
- +
-
-
- +
- + - + :value="old('carbohydrates')"/>
-
- or -
- - +
- + - + :value="old('protein')"/>
diff --git a/resources/views/foods/index.blade.php b/resources/views/foods/index.blade.php index 3082d4a..824eb6d 100644 --- a/resources/views/foods/index.blade.php +++ b/resources/views/foods/index.blade.php @@ -17,20 +17,21 @@
@foreach ($foods as $food)
-
-
+
{{ $food->name }}@if($food->detail), {{ $food->detail }}@endif +
+ @if($food->brand) +
+ {{ $food->brand }}
-
- @if ($food->unit_weight) - {{ $food->unit_weight }}g each - @else - {{ $food->cup_weight }}g per cup - @endif -
+ @endif +
+ Serving size {{ $food->serving_size }} + {{ $food->serving_unit }} + ({{ $food->serving_weight }}g)
-
Amount per 100g
+
Amount per serving
Calories
{{$food->calories}}
Fat