Deleted related ingredient amounts on food delete

This commit is contained in:
Christopher C. Wells 2021-02-19 05:53:53 -08:00
parent ae27a2e4bd
commit 4856ed4b70
2 changed files with 30 additions and 11 deletions

View File

@ -125,8 +125,9 @@ class FoodController extends Controller
*/ */
public function destroy(Food $food): RedirectResponse public function destroy(Food $food): RedirectResponse
{ {
if (!empty($food->ingredientAmountRelationships)) { // Remove the food from any recipes.
return back()->withErrors('Cannot delete: this food is used in recipes.'); foreach ($food->ingredientAmountRelationships as $ia) {
$ia->delete();
} }
$food->delete(); $food->delete();
return redirect(route('foods.index')) return redirect(route('foods.index'))

View File

@ -12,26 +12,44 @@
<form method="POST" action="{{ route('foods.destroy', $food) }}"> <form method="POST" action="{{ route('foods.destroy', $food) }}">
@method('delete') @method('delete')
@csrf @csrf
<div class="text-lg pb-3"> <div class="pb-3">
Are you sure what to delete <span class="font-extrabold">{{ $food->name }}</span>? <div class="text-lg">Are you sure what to delete <span class="font-extrabold">{{ $food->name }}</span>?</div>
<div class="flex flex-col space-y-2 mt-2 mb-2 text-lg"> <div class="flex flex-col space-y-2 mt-2 text-lg">
<div> @if($food->detail)
<span class="font-bold">Detail:</span> <div>
<span>{{ $food->detail }}</span> <span class="font-bold">Detail:</span>
</div> <span>{{ $food->detail }}</span>
</div>
@endif
@if($food->brand) @if($food->brand)
<div> <div>
<span class="font-bold">Brand:</span> <span class="font-bold">Brand:</span>
<span>{{ $food->brand }}</span> <span>{{ $food->brand }}</span>
</div> </div>
@endif @endif
@if(!$food->ingredientAmountRelationships->isEmpty())
<div class="flex space-x-2 items-center text-lg">
<div class="text-yellow-500">
<svg class="h-8 w-8" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M10 1.944A11.954 11.954 0 012.166 5C2.056 5.649 2 6.319 2 7c0 5.225 3.34 9.67 8 11.317C14.66 16.67 18 12.225 18 7c0-.682-.057-1.35-.166-2.001A11.954 11.954 0 0110 1.944zM11 14a1 1 0 11-2 0 1 1 0 012 0zm0-7a1 1 0 10-2 0v3a1 1 0 102 0V7z" clip-rule="evenodd" />
</svg>
</div>
<div>Deleting this food will also remove it from the following recipes:</div>
</div>
<ul class="list-disc list-inside ml-3 space-y-1">
@foreach ($food->ingredientAmountRelationships as $ia)
<li> <a class="text-gray-500 hover:text-gray-700"
href="{{ route('recipes.show', $ia->parent) }}">{{ $ia->parent->name }}</a></li>
@endforeach
</ul>
@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">
Yes, delete Yes, delete
</x-inputs.button> </x-inputs.button>
<a class="ml-3 text-gray-500 hover:text-gray-700 hover:border-gray-300" <a class="ml-3 text-gray-500 hover:text-gray-700" href="{{ route('foods.show', $food) }}">
href="{{ route('foods.show', $food) }}">No, do not delete</a> No, do not delete</a>
</form> </form>
</div> </div>
</div> </div>