Set formatted serving size to null when empty

This commit is contained in:
Christopher C. Wells 2021-03-08 15:06:21 -08:00
parent fafc4c9e33
commit 65e3e9c776
2 changed files with 7 additions and 10 deletions

View File

@ -156,8 +156,12 @@ final class Food extends Model
/**
* Get the serving size as a formatted string (e.g. 0.5 = 1/2).
*/
public function getServingSizeFormattedAttribute(): string {
return Number::fractionStringFromFloat($this->serving_size);
public function getServingSizeFormattedAttribute(): ?string {
$result = null;
if (!empty($this->serving_size)) {
$result = Number::fractionStringFromFloat($this->serving_size);
}
return $result;
}
/**

View File

@ -46,13 +46,6 @@
<div class="flex flex-col space-y-4 sm:flex-row sm:space-x-4 sm:space-y-0">
<!-- Serving size -->
@php
if (!empty($food->serving_size)) {
$old_value = \App\Support\Number::fractionStringFromFloat($food->serving_size);
} else {
$old_value = null;
}
@endphp
<div>
<x-inputs.label for="serving_size" value="Serving size"/>
@ -61,7 +54,7 @@
type="text"
name="serving_size"
size="10"
:value="old('serving_size', $old_value)"
:value="old('serving_size', $food->serving_size_formatted)"
required/>
</div>