From ae27a2e4bd6432851f0e938b3306aa69702874fa Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 17 Feb 2021 06:05:29 -0800 Subject: [PATCH] Fix Ingredient/HasIngredient trait relationships --- app/Http/Controllers/FoodController.php | 2 +- app/Models/Food.php | 4 ++-- app/Models/IngredientAmount.php | 6 +----- app/Models/Recipe.php | 4 ++-- app/Models/Traits/HasIngredients.php | 4 +++- app/Models/Traits/Ingredient.php | 6 +++--- resources/views/foods/delete.blade.php | 16 +++++++++++----- 7 files changed, 23 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/FoodController.php b/app/Http/Controllers/FoodController.php index a92e2ec..078326a 100644 --- a/app/Http/Controllers/FoodController.php +++ b/app/Http/Controllers/FoodController.php @@ -125,7 +125,7 @@ class FoodController extends Controller */ public function destroy(Food $food): RedirectResponse { - if ($food->ingredientAmountChildren()->count()) { + if (!empty($food->ingredientAmountRelationships)) { return back()->withErrors('Cannot delete: this food is used in recipes.'); } $food->delete(); diff --git a/app/Models/Food.php b/app/Models/Food.php index 1de55f3..145c681 100644 --- a/app/Models/Food.php +++ b/app/Models/Food.php @@ -56,8 +56,8 @@ use Spatie\Tags\HasTags; * @property-read string $serving_size_formatted * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmounts * @property-read int|null $ingredient_amounts_count - * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmountChildren - * @property-read int|null $ingredient_amount_children_count + * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmountRelationships + * @property-read int|null $ingredient_amount_relationships_count * @property-read string $type * @property \Illuminate\Database\Eloquent\Collection|\Spatie\Tags\Tag[] $tags * @property-read int|null $tags_count diff --git a/app/Models/IngredientAmount.php b/app/Models/IngredientAmount.php index e93be6d..2eb869d 100644 --- a/app/Models/IngredientAmount.php +++ b/app/Models/IngredientAmount.php @@ -42,6 +42,7 @@ use Illuminate\Support\Pluralizer; * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereWeight($value) * @mixin \Eloquent * @property-read string|null $unit_formatted + * @property-read string $nutrients_summary */ final class IngredientAmount extends Model { @@ -65,11 +66,6 @@ final class IngredientAmount extends Model 'weight' => 'int', ]; - /** - * @inheritdoc - */ - protected $with = ['ingredient']; - /** * Nutrient calculation methods. */ diff --git a/app/Models/Recipe.php b/app/Models/Recipe.php index 4d11ed4..315b7fa 100644 --- a/app/Models/Recipe.php +++ b/app/Models/Recipe.php @@ -44,8 +44,8 @@ use Spatie\Tags\HasTags; * @property-read int|null $ingredient_amounts_count * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredients * @property-read int|null $ingredients_count - * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmountChildren - * @property-read int|null $ingredient_amount_children_count + * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmountRelationships + * @property-read int|null $ingredient_amount_relationships_count * @property-read string $type * @property \Illuminate\Database\Eloquent\Collection|\Spatie\Tags\Tag[] $tags * @property-read int|null $tags_count diff --git a/app/Models/Traits/HasIngredients.php b/app/Models/Traits/HasIngredients.php index baca553..6aeda25 100644 --- a/app/Models/Traits/HasIngredients.php +++ b/app/Models/Traits/HasIngredients.php @@ -23,7 +23,9 @@ trait HasIngredients * Get all of the ingredients. */ public function ingredientAmounts(): MorphMany { - return $this->morphMany(IngredientAmount::class, 'parent')->orderBy('weight'); + return $this->morphMany(IngredientAmount::class, 'parent') + ->with('ingredient') + ->orderBy('weight'); } /** diff --git a/app/Models/Traits/Ingredient.php b/app/Models/Traits/Ingredient.php index 80c6dd3..36bce94 100644 --- a/app/Models/Traits/Ingredient.php +++ b/app/Models/Traits/Ingredient.php @@ -3,7 +3,7 @@ namespace App\Models\Traits; use App\Models\IngredientAmount; -use Illuminate\Database\Eloquent\Relations\MorphToMany; +use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Support\Collection; trait Ingredient @@ -27,8 +27,8 @@ trait Ingredient /** * Get all of the ingredient amounts associated with the ingredient. */ - public function ingredientAmountChildren(): MorphToMany { - return $this->morphToMany(IngredientAmount::class, 'ingredient'); + public function ingredientAmountRelationships(): MorphMany { + return $this->morphMany(IngredientAmount::class, 'ingredient')->with('parent'); } /** diff --git a/resources/views/foods/delete.blade.php b/resources/views/foods/delete.blade.php index 15a3434..72d2001 100644 --- a/resources/views/foods/delete.blade.php +++ b/resources/views/foods/delete.blade.php @@ -14,11 +14,17 @@ @csrf
Are you sure what to delete {{ $food->name }}? -
-
Detail:
-
{{ $food->detail ?? 'None' }}
-
Brand:
-
{{ $food->brand ?? 'None' }}
+
+
+ Detail: + {{ $food->detail }} +
+ @if($food->brand) +
+ Brand: + {{ $food->brand }} +
+ @endif