diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index fa304e0..d6a1abd 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -2,8 +2,9 @@ namespace App\Http\Controllers; -use Illuminate\Http\Request; use \App\Item; +use \App\User; +use Illuminate\Http\Request; class ItemController extends Controller { @@ -19,7 +20,6 @@ class ItemController extends Controller public function index() { - //TODO: Fazer innerjoint com tabela de users por item $items = Item::where('user_id', \Auth::id())->get(); return view('item.index', compact('items')); } @@ -40,19 +40,35 @@ class ItemController extends Controller ] ); - $authUser = \App\User::find(\Auth::id()); + $authUser = User::find(\Auth::id()); $authUser->items()->create(['name' => request('item'), 'product_id' => request('product_id')]); return redirect('product/'.request('product_id')); } + public function patch(Request $request) + { + $request->validate(['item' => 'required', 'name' => 'required']); + $item = User::find(\Auth::id())->items()->find(request('item')); + $item->name = request('name'); + $item->save(); + return redirect('item/'.request('item')); + } + public function delete(Request $request) { $request->validate(['item' => 'required']); - $item = \App\User::find(\Auth::id())->items()->find(request('item')); + $item = User::find(\Auth::id()) + ->items()->with('users')->find(request('item')); $product = $item->product_id; + + //Detach users from this item + foreach ($item->users as $user) { + User::findOrFail($user->id)->items()->detach($item->id); + } + + //Delete item $item->delete(); return redirect('product/' . $product); } - -} +} \ No newline at end of file diff --git a/resources/views/item/editButton.blade.php b/resources/views/item/editButton.blade.php index a013282..d934c0a 100644 --- a/resources/views/item/editButton.blade.php +++ b/resources/views/item/editButton.blade.php @@ -23,7 +23,7 @@ {{ csrf_field() }} - + diff --git a/resources/views/product/buttons.blade.php b/resources/views/product/buttons.blade.php index 8236b64..1d32f34 100644 --- a/resources/views/product/buttons.blade.php +++ b/resources/views/product/buttons.blade.php @@ -3,20 +3,44 @@ - @if ($product->user_id == \Auth::id()) -
- @method('PATCH') - -
- @endif - - -
- @method('DELETE') - + @method('PATCH') +
+ +
+ {{ csrf_field() }} + @method('DELETE') + +
+ + + + + \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index b7fb896..4776feb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -22,6 +22,7 @@ Route::post('/product', 'ProductController@store')->middleware('auth'); Route::get('/item', 'ItemController@index')->middleware('auth'); Route::get('/item/{item}', 'ItemController@show')->middleware('auth'); Route::post('/item', 'ItemController@store')->middleware('auth'); +Route::patch('/item', 'ItemController@patch')->middleware('auth'); Route::delete('/item', 'ItemController@delete')->middleware('auth'); Route::post('/take', 'TakeController@store')->middleware('auth');