Including Item Delete

This commit is contained in:
Bruno F. Fontes 2018-09-12 21:27:04 -03:00
parent a3fe6f719d
commit 52eda15057
2 changed files with 15 additions and 4 deletions

View File

@ -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 $user = \App\User::find(\Auth::id());
\DB::table('item_user')->insert([ 'user_id' => \Auth::id(), 'item_id' => $id]); $user->items()->create(['name' => request('item'), 'product_id' => request('product_id')]);
return redirect('product/'.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);
}
} }

View File

@ -12,9 +12,11 @@
<!-- DELETE BUTTON --> <!-- DELETE BUTTON -->
<form action="/item/{{$item->id}}" method="POST"> <form action="/item/" method="POST">
@method('DELETE') @method('DELETE')
<button type="button" class="btn-sm btn-danger">Delete</button> {{ csrf_field() }}
<input type="hidden" name="item" value="{{$item->id}}">
<button type="submit" class="btn-sm btn-danger">Delete</button>
</form> </form>