From 52eda1505771ccccb18d41d7348b5cddd78961b9 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() }} + +