diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 4d3fa2d..ae1116b 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -156,6 +156,7 @@ class RecipeController extends Controller 'description' => 'nullable|string', 'source' => 'nullable|string', 'servings' => 'required|numeric', + 'weight' => 'nullable|numeric', 'ingredients.amount' => ['required', 'array', new ArrayNotEmpty], 'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsDecimalOrFraction], 'ingredients.unit' => ['required', 'array'], @@ -185,6 +186,7 @@ class RecipeController extends Controller 'description' => $input['description'], 'source' => $input['source'], 'servings' => (int) $input['servings'], + 'weight' => $input['weight'], ]); try { diff --git a/app/JsonApi/Schemas/RecipeSchema.php b/app/JsonApi/Schemas/RecipeSchema.php index 7a2fdeb..250dc3c 100644 --- a/app/JsonApi/Schemas/RecipeSchema.php +++ b/app/JsonApi/Schemas/RecipeSchema.php @@ -31,6 +31,8 @@ class RecipeSchema extends SchemaProvider 'description' => $resource->description, 'source' => $resource->source, 'servings' => $resource->servings, + 'weight' => $resource->weight, + 'serving_weight' => $resource->serving_weight, 'caloriesPerServing' => $resource->caloriesPerServing(), 'carbohydratesPerServing' => $resource->carbohydratesPerServing(), 'cholesterolPerServing' => $resource->cholesterolPerServing(), diff --git a/app/Models/Recipe.php b/app/Models/Recipe.php index a7f0c76..f4e35ac 100644 --- a/app/Models/Recipe.php +++ b/app/Models/Recipe.php @@ -6,6 +6,7 @@ use App\Models\Traits\HasIngredients; use App\Models\Traits\Ingredient; use App\Models\Traits\Journalable; use App\Models\Traits\Sluggable; +use App\Support\Number; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -65,6 +66,7 @@ final class Recipe extends Model 'description', 'source', 'servings', + 'weight', ]; /** @@ -72,6 +74,7 @@ final class Recipe extends Model */ protected $casts = [ 'servings' => 'int', + 'weight' => 'float', ]; /** @@ -86,6 +89,23 @@ final class Recipe extends Model 'sodiumPerServing', ]; + /** + * @inheritdoc + */ + protected $appends = [ + 'serving_weight', + ]; + + /** + * Get the serving weight (rounded). + */ + public function getServingWeightAttribute(): ?float { + if (empty($this->weight)) { + return null; + } + return round($this->weight / $this->servings, 2); + } + /** * Get the steps for this Recipe. */ diff --git a/database/migrations/2020_12_21_215932_create_recipes_table.php b/database/migrations/2020_12_21_215932_create_recipes_table.php index a487360..b6cd285 100644 --- a/database/migrations/2020_12_21_215932_create_recipes_table.php +++ b/database/migrations/2020_12_21_215932_create_recipes_table.php @@ -20,6 +20,7 @@ class CreateRecipesTable extends Migration $table->longText('description')->nullable(); $table->string('source')->nullable(); $table->unsignedInteger('servings'); + $table->unsignedFloat('weight')->nullable(); $table->timestamps(); }); } diff --git a/database/migrations/2021_02_11_192732_add_weight_column_to_recipes.php b/database/migrations/2021_02_11_192732_add_weight_column_to_recipes.php new file mode 100644 index 0000000..290ca79 --- /dev/null +++ b/database/migrations/2021_02_11_192732_add_weight_column_to_recipes.php @@ -0,0 +1,32 @@ +unsignedFloat('weight')->nullable()->after('servings'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('recipes', function (Blueprint $table) { + $table->dropColumn('weight'); + }); + } +} diff --git a/resources/views/recipes/edit.blade.php b/resources/views/recipes/edit.blade.php index 5f5b646..0d8006f 100644 --- a/resources/views/recipes/edit.blade.php +++ b/resources/views/recipes/edit.blade.php @@ -17,10 +17,9 @@
-
@@ -29,33 +28,41 @@
-
+ +
+ + + +
+ +
-
-
-
+
-
diff --git a/resources/views/recipes/index.blade.php b/resources/views/recipes/index.blade.php index 9ff2c16..3725e23 100644 --- a/resources/views/recipes/index.blade.php +++ b/resources/views/recipes/index.blade.php @@ -23,10 +23,14 @@
-
+
-
Amount per serving
+
+
Serving weight
+
+
+
Amount per serving
Calories
diff --git a/resources/views/recipes/show.blade.php b/resources/views/recipes/show.blade.php index c84a8c0..fc3d8e8 100644 --- a/resources/views/recipes/show.blade.php +++ b/resources/views/recipes/show.blade.php @@ -50,6 +50,12 @@
Nutrition Facts
{{ $recipe->servings }} {{ \Illuminate\Support\Str::plural('serving', $recipe->servings ) }}
+ @if($recipe->serving_weight) +
+
Serving weight
+
{{ $recipe->serving_weight }}g
+
+ @endif
Amount per serving
Calories