Creating relationships between users/items/products

This commit is contained in:
2018-09-12 02:31:03 -03:00
parent 0f15e60c95
commit a905d98304
8 changed files with 25 additions and 27 deletions

View File

@@ -9,19 +9,17 @@ class ItemController extends Controller
{
public function show($id)
{
$item = \DB::table('items')
->join('products', 'items.product_id', '=', 'products.id')
->where([['products.admin_id', \Auth::id()], ['items.id', $id]])
->select('items.*', 'products.admin_id')
->get();
$otherItems = Item::where([['product_id', $item[0]->product_id], ['id', '!=', $id]])->get();
$item = Item::find($id);
if (!$item || $item->product->user_id != \Auth::id()) return back();
$otherItems = Item::where([['product_id', $item->product_id], ['id', '!=', $id]])->get();
return view('item', compact('item', 'otherItems'));
}
public function index()
{
//TODO: Fazer innerjoint com tabela de users por item
$items = Item::where('admin_id', \Auth::id())->get();
$items = Item::where('user_id', \Auth::id())->get();
return view('item.index', compact('items'));
}
@@ -40,6 +38,7 @@ class ItemController extends Controller
'product_id' => 'required'
]
);
$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]);