Fix Ingredient/HasIngredient trait relationships

This commit is contained in:
Christopher C. Wells 2021-02-17 06:05:29 -08:00
parent b8ae2f5473
commit ae27a2e4bd
7 changed files with 23 additions and 19 deletions

View File

@ -125,7 +125,7 @@ class FoodController extends Controller
*/ */
public function destroy(Food $food): RedirectResponse 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.'); return back()->withErrors('Cannot delete: this food is used in recipes.');
} }
$food->delete(); $food->delete();

View File

@ -56,8 +56,8 @@ use Spatie\Tags\HasTags;
* @property-read string $serving_size_formatted * @property-read string $serving_size_formatted
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmounts * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmounts
* @property-read int|null $ingredient_amounts_count * @property-read int|null $ingredient_amounts_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmountChildren * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmountRelationships
* @property-read int|null $ingredient_amount_children_count * @property-read int|null $ingredient_amount_relationships_count
* @property-read string $type * @property-read string $type
* @property \Illuminate\Database\Eloquent\Collection|\Spatie\Tags\Tag[] $tags * @property \Illuminate\Database\Eloquent\Collection|\Spatie\Tags\Tag[] $tags
* @property-read int|null $tags_count * @property-read int|null $tags_count

View File

@ -42,6 +42,7 @@ use Illuminate\Support\Pluralizer;
* @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereWeight($value) * @method static \Illuminate\Database\Eloquent\Builder|IngredientAmount whereWeight($value)
* @mixin \Eloquent * @mixin \Eloquent
* @property-read string|null $unit_formatted * @property-read string|null $unit_formatted
* @property-read string $nutrients_summary
*/ */
final class IngredientAmount extends Model final class IngredientAmount extends Model
{ {
@ -65,11 +66,6 @@ final class IngredientAmount extends Model
'weight' => 'int', 'weight' => 'int',
]; ];
/**
* @inheritdoc
*/
protected $with = ['ingredient'];
/** /**
* Nutrient calculation methods. * Nutrient calculation methods.
*/ */

View File

@ -44,8 +44,8 @@ use Spatie\Tags\HasTags;
* @property-read int|null $ingredient_amounts_count * @property-read int|null $ingredient_amounts_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredients * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredients
* @property-read int|null $ingredients_count * @property-read int|null $ingredients_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmountChildren * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmountRelationships
* @property-read int|null $ingredient_amount_children_count * @property-read int|null $ingredient_amount_relationships_count
* @property-read string $type * @property-read string $type
* @property \Illuminate\Database\Eloquent\Collection|\Spatie\Tags\Tag[] $tags * @property \Illuminate\Database\Eloquent\Collection|\Spatie\Tags\Tag[] $tags
* @property-read int|null $tags_count * @property-read int|null $tags_count

View File

@ -23,7 +23,9 @@ trait HasIngredients
* Get all of the ingredients. * Get all of the ingredients.
*/ */
public function ingredientAmounts(): MorphMany { public function ingredientAmounts(): MorphMany {
return $this->morphMany(IngredientAmount::class, 'parent')->orderBy('weight'); return $this->morphMany(IngredientAmount::class, 'parent')
->with('ingredient')
->orderBy('weight');
} }
/** /**

View File

@ -3,7 +3,7 @@
namespace App\Models\Traits; namespace App\Models\Traits;
use App\Models\IngredientAmount; use App\Models\IngredientAmount;
use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
trait Ingredient trait Ingredient
@ -27,8 +27,8 @@ trait Ingredient
/** /**
* Get all of the ingredient amounts associated with the ingredient. * Get all of the ingredient amounts associated with the ingredient.
*/ */
public function ingredientAmountChildren(): MorphToMany { public function ingredientAmountRelationships(): MorphMany {
return $this->morphToMany(IngredientAmount::class, 'ingredient'); return $this->morphMany(IngredientAmount::class, 'ingredient')->with('parent');
} }
/** /**

View File

@ -14,11 +14,17 @@
@csrf @csrf
<div class="text-lg pb-3"> <div class="text-lg pb-3">
Are you sure what to delete <span class="font-extrabold">{{ $food->name }}</span>? Are you sure what to delete <span class="font-extrabold">{{ $food->name }}</span>?
<div class="grid grid-cols-2 w-max"> <div class="flex flex-col space-y-2 mt-2 mb-2 text-lg">
<div class="font-bold pr-2">Detail:</div> <div>
<div>{{ $food->detail ?? 'None' }}</div> <span class="font-bold">Detail:</span>
<div class="font-bold pr-2">Brand:</div> <span>{{ $food->detail }}</span>
<div>{{ $food->brand ?? 'None' }}</div> </div>
@if($food->brand)
<div>
<span class="font-bold">Brand:</span>
<span>{{ $food->brand }}</span>
</div>
@endif
</div> </div>
</div> </div>
<x-inputs.button class="bg-red-800 hover:bg-red-700"> <x-inputs.button class="bg-red-800 hover:bg-red-700">