From 1408e558d70d67feebb693bcf5ad7ad79a5f5222 Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Wed, 12 Sep 2018 21:27:04 -0300 Subject: [PATCH] Including Item Delete --- app/Http/Controllers/ItemController.php | 13 +++++++++++-- resources/views/item/buttons.blade.php | 6 ++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index cc95a48..1094b15 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -39,10 +39,19 @@ class ItemController extends Controller ] ); - $id = Item::insertGetId(['name' => request('item'), 'product_id' => request('product_id')]); //Just remember to add the fillable on Model to make this work - \DB::table('item_user')->insert([ 'user_id' => \Auth::id(), 'item_id' => $id]); + $user = \App\User::find(\Auth::id()); + $user->items()->create(['name' => request('item'), 'product_id' => request('product_id')]); return redirect('product/'.request('product_id')); } + public function delete(Request $request) + { + $request->validate(['item' => 'required']); + $item = \App\User::find(\Auth::id())->items()->find(request('item')); + $product = $item->product_id; + $item->delete(); + return redirect('product/' . $product); + } + } diff --git a/resources/views/item/buttons.blade.php b/resources/views/item/buttons.blade.php index 6345bef..3435755 100644 --- a/resources/views/item/buttons.blade.php +++ b/resources/views/item/buttons.blade.php @@ -12,9 +12,11 @@ -
+ @method('DELETE') - + {{ csrf_field() }} + +