diff --git a/app/JsonApi/Schemas/IngredientAmountSchema.php b/app/JsonApi/Schemas/IngredientAmountSchema.php index 234a7b3..bb71bc6 100644 --- a/app/JsonApi/Schemas/IngredientAmountSchema.php +++ b/app/JsonApi/Schemas/IngredientAmountSchema.php @@ -36,6 +36,7 @@ class IngredientAmountSchema extends SchemaProvider 'protein' => $resource->protein(), 'sodium' => $resource->sodium(), 'detail' => $resource->detail, + 'nutrientsSummary' => $resource->nutrients_summary, 'weight' => $resource->weight, 'createdAt' => $resource->created_at, 'updatedAt' => $resource->updated_at, diff --git a/app/Models/IngredientAmount.php b/app/Models/IngredientAmount.php index a5ff8ec..e93be6d 100644 --- a/app/Models/IngredientAmount.php +++ b/app/Models/IngredientAmount.php @@ -85,7 +85,11 @@ final class IngredientAmount extends Model /** * @inheritdoc */ - protected $appends = ['amount_formatted', 'unit_formatted']; + protected $appends = [ + 'amount_formatted', + 'nutrients_summary', + 'unit_formatted' + ]; /** * Get the amount as a formatted string (e.g. 0.5 = 1/2). @@ -94,6 +98,20 @@ final class IngredientAmount extends Model return Number::fractionStringFromFloat($this->amount); } + /** + * Get a simple string summary of nutrients for the ingredient amount. + * + * TODO: Move to HasIngredients trait. + */ + public function getNutrientsSummaryAttribute(): string { + $summary = []; + foreach (Nutrients::$all as $nutrient) { + $amount = round($this->{$nutrient['value']}(), 2); + $summary[] = "{$nutrient['label']}: {$amount}"; + } + return implode(', ', $summary); + } + /** * Get a "formatted" unit. * diff --git a/resources/views/recipes/show.blade.php b/resources/views/recipes/show.blade.php index fc3d8e8..bf09dbd 100644 --- a/resources/views/recipes/show.blade.php +++ b/resources/views/recipes/show.blade.php @@ -16,7 +16,7 @@
-
+
@if(!$recipe->tags->isEmpty())
Tags: @@ -27,24 +27,33 @@

Description

{{ $recipe->description }}
@endif -

Ingredients

- @foreach($recipe->ingredientAmounts as $ia) -
-
{{ \App\Support\Number::fractionStringFromFloat($ia->amount) }}
- @if($ia->unitFormatted)
{{ $ia->unitFormatted }}
@endif +

+ Ingredients + [toggle nutrients] +

+
+ @foreach($recipe->ingredientAmounts as $ia)
- @if($ia->ingredient->type === \App\Models\Recipe::class) - - {{ $ia->ingredient->name }} - - @else - {{ $ia->ingredient->name }}@if($ia->ingredient->detail), {{ $ia->ingredient->detail }}@endif - @endif +
+
{{ \App\Support\Number::fractionStringFromFloat($ia->amount) }}
+ @if($ia->unitFormatted)
{{ $ia->unitFormatted }}
@endif +
+ @if($ia->ingredient->type === \App\Models\Recipe::class) + + {{ $ia->ingredient->name }} + + @else + {{ $ia->ingredient->name }}@if($ia->ingredient->detail), {{ $ia->ingredient->detail }}@endif + @endif +
+ @if($ia->detail)
{{ $ia->detail }}
@endif +
+
{{ $ia->nutrients_summary }}
- @if($ia->detail)
{{ $ia->detail }}
@endif -
- @endforeach + @endforeach +