Adding edit function to ITEM

Now an item can be renamed.
This commit is contained in:
Bruno F. Fontes 2018-09-16 16:43:03 -03:00
parent a3a7821578
commit d8847256fd
4 changed files with 59 additions and 18 deletions

View File

@ -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);
}
}
}

View File

@ -23,7 +23,7 @@
{{ csrf_field() }}
<input type="hidden" name="item" value="{{$item->id}}">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
<button type="submit" class="btn btn-danger">Edit</button>
</div>
</form>

View File

@ -3,20 +3,44 @@
<!-- EDIT BUTTON -->
@if ($product->user_id == \Auth::id())
<form action="/product" method="POST">
@method('PATCH')
<button type="submit" class="btn-sm btn-secondary mr-1">Edit</button>
</form>
@endif
<!-- DELETE BUTTON -->
<form action="/product" method="POST">
@method('DELETE')
<button type="submit" class="btn-sm btn-danger">Delete</button>
@method('PATCH')
<button type="submit" class="btn btn-sm btn-secondary mr-1">Edit</button>
</form>
<!-- DELETE BUTTON -->
<form class="delete" action="/product" method="POST">
{{ csrf_field() }}
@method('DELETE')
<button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#deleteModal">Delete</button>
</form>
</div>
<!-- MODAL - DELETE CONFIRMATION -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Confirmation...</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
Would you like to delete the product <strong>{{$product->name}}</strong> and it's items?
You will not be able to restore it after deletion.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<form class="delete" action="/product" method="POST">
{{ csrf_field() }}
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</div>
</div>
</div>
</div>
</span>

View File

@ -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');