2018-09-09 03:26:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use \App\Item;
|
2018-09-16 19:43:03 +00:00
|
|
|
use \App\User;
|
|
|
|
use Illuminate\Http\Request;
|
2018-09-09 03:26:57 +00:00
|
|
|
|
|
|
|
class ItemController extends Controller
|
|
|
|
{
|
2018-09-11 00:29:32 +00:00
|
|
|
public function show($id)
|
|
|
|
{
|
2018-09-12 05:31:03 +00:00
|
|
|
$item = Item::find($id);
|
|
|
|
if (!$item || $item->product->user_id != \Auth::id()) return back();
|
2018-09-14 00:16:16 +00:00
|
|
|
$users = $item->users()->get();
|
2018-09-12 05:31:03 +00:00
|
|
|
|
|
|
|
$otherItems = Item::where([['product_id', $item->product_id], ['id', '!=', $id]])->get();
|
2018-09-14 00:16:16 +00:00
|
|
|
return view('item', compact('item', 'otherItems', 'users'));
|
2018-09-11 00:29:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 03:26:57 +00:00
|
|
|
public function index()
|
|
|
|
{
|
2018-09-12 05:31:03 +00:00
|
|
|
$items = Item::where('user_id', \Auth::id())->get();
|
2018-09-11 00:29:32 +00:00
|
|
|
return view('item.index', compact('items'));
|
2018-09-09 03:26:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 00:29:32 +00:00
|
|
|
/**
|
|
|
|
* Stores the included item into database
|
|
|
|
* As the items are included on the Product view,
|
|
|
|
* it must return to there after inclusion
|
|
|
|
*
|
|
|
|
* @return (view) The product view
|
|
|
|
*/
|
2018-09-11 23:48:09 +00:00
|
|
|
public function store(Request $request)
|
2018-09-09 03:26:57 +00:00
|
|
|
{
|
2018-09-11 23:48:09 +00:00
|
|
|
$request->validate(
|
|
|
|
[
|
|
|
|
'item' => 'required',
|
|
|
|
'product_id' => 'required'
|
|
|
|
]
|
|
|
|
);
|
2018-09-12 05:31:03 +00:00
|
|
|
|
2018-09-16 19:43:03 +00:00
|
|
|
$authUser = User::find(\Auth::id());
|
2018-09-14 00:16:16 +00:00
|
|
|
$authUser->items()->create(['name' => request('item'), 'product_id' => request('product_id')]);
|
2018-09-11 00:29:32 +00:00
|
|
|
|
2018-09-11 23:48:09 +00:00
|
|
|
return redirect('product/'.request('product_id'));
|
2018-09-09 03:26:57 +00:00
|
|
|
}
|
2018-09-11 00:29:32 +00:00
|
|
|
|
2018-09-16 19:43:03 +00:00
|
|
|
public function patch(Request $request)
|
|
|
|
{
|
|
|
|
$request->validate(['item' => 'required', 'name' => 'required']);
|
2018-09-16 23:31:27 +00:00
|
|
|
$item = User::findOrFail(\Auth::id())->items()->find(request('item'));
|
2018-09-16 19:43:03 +00:00
|
|
|
$item->name = request('name');
|
|
|
|
$item->save();
|
|
|
|
return redirect('item/'.request('item'));
|
|
|
|
}
|
|
|
|
|
2018-09-13 00:27:04 +00:00
|
|
|
public function delete(Request $request)
|
|
|
|
{
|
|
|
|
$request->validate(['item' => 'required']);
|
2018-09-17 03:51:41 +00:00
|
|
|
$item = User::findOrFail(\Auth::id())
|
2018-09-16 19:43:03 +00:00
|
|
|
->items()->with('users')->find(request('item'));
|
2018-09-13 00:27:04 +00:00
|
|
|
$product = $item->product_id;
|
2018-09-16 19:43:03 +00:00
|
|
|
|
2018-09-17 03:51:41 +00:00
|
|
|
Item::deleteAndDetach($item);
|
2018-09-13 00:27:04 +00:00
|
|
|
return redirect('product/' . $product);
|
|
|
|
}
|
2018-09-16 19:43:03 +00:00
|
|
|
}
|