mirror of
https://github.com/brunofontes/shareit.git
synced 2025-11-15 07:50:54 -03:00
Imrpoving Item, Product and Users
This commit is contained in:
@@ -23,7 +23,11 @@ class HomeController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$items = \App\Item::where('userID');
|
||||
$items = \DB::table('items')
|
||||
->join('item_user', 'item_user.item_id', '=', 'items.id')
|
||||
->where('item_user.user_id', \Auth::id())
|
||||
->select('items.*')
|
||||
->get();
|
||||
return view('home', compact('items'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,15 +9,19 @@ class ItemController extends Controller
|
||||
{
|
||||
public function show($id)
|
||||
{
|
||||
//TODO: Fazer innerjoint com tabela de users por item
|
||||
$item = Item::where([['id', $id], ['userID', \Auth::id()]]);
|
||||
return view('item', compact('item'));
|
||||
$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();
|
||||
return view('item', compact('item', 'otherItems'));
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
//TODO: Fazer innerjoint com tabela de users por item
|
||||
$items = Item::where('adminID', \Auth::id())->get();
|
||||
$items = Item::where('admin_id', \Auth::id())->get();
|
||||
return view('item.index', compact('items'));
|
||||
}
|
||||
|
||||
@@ -28,12 +32,18 @@ class ItemController extends Controller
|
||||
*
|
||||
* @return (view) The product view
|
||||
*/
|
||||
public function store()
|
||||
public function store(Request $request)
|
||||
{
|
||||
$this->validate(request(), ['name' => 'required']);
|
||||
Item::create(['name' => request('item'), 'productID' => request('productID')]); //Just remember to add the fillable on Model to make this work
|
||||
$request->validate(
|
||||
[
|
||||
'item' => 'required',
|
||||
'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]);
|
||||
|
||||
return redirect('product');
|
||||
return redirect('product/'.request('product_id'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ class ProductController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$products = Product::where('adminID', \Auth::id())->get();
|
||||
$products = Product::where('admin_id', \Auth::id())->get();
|
||||
return view('product.index', compact('products'));
|
||||
}
|
||||
|
||||
@@ -21,14 +21,7 @@ class ProductController extends Controller
|
||||
*/
|
||||
public function store()
|
||||
{
|
||||
/*
|
||||
$product = new Product;
|
||||
$product->name = request('product');
|
||||
$product->adminID = $userID;
|
||||
$product->save();
|
||||
*/
|
||||
Product::create(['name' => request('product'), 'adminID' => \Auth::id()]); //Just remember to add the fillable on Model to make this work
|
||||
|
||||
Product::create(['name' => request('product'), 'admin_id' => \Auth::id()]); //Just remember to add the fillable on Model to make this work
|
||||
return redirect('product');
|
||||
}
|
||||
|
||||
@@ -48,12 +41,7 @@ class ProductController extends Controller
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$product = Product::find($id);
|
||||
$items = \DB::table('items')
|
||||
->join('products', 'items.productID', '=', 'products.id')
|
||||
->where('products.adminID', \Auth::id())
|
||||
->select('items.*', 'products.*')
|
||||
->get();
|
||||
return view('product.show', compact('items', 'product'));
|
||||
$product = Product::where('admin_id', \Auth::id())->find($id);
|
||||
return view('product.show', compact('product'));
|
||||
}
|
||||
}
|
||||
|
||||
14
app/Item.php
14
app/Item.php
@@ -6,12 +6,18 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Item extends Model
|
||||
{
|
||||
//
|
||||
public function free($productID, $userID)
|
||||
protected $fillable = ['product_id', 'name'];
|
||||
|
||||
public function product()
|
||||
{
|
||||
return $this->belongsTo(Product::class);
|
||||
}
|
||||
|
||||
public function free($product_id, $user_id)
|
||||
{
|
||||
return $query->where([
|
||||
['userID', $userID],
|
||||
['productID', $productID],
|
||||
['user_id', $user_id],
|
||||
['product_id', $product_id],
|
||||
['usedBy', ''],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,23 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Product extends Model
|
||||
{
|
||||
protected $fillable = ['adminID', 'name'];
|
||||
//
|
||||
protected $fillable = ['admin_id', 'name'];
|
||||
|
||||
public function items()
|
||||
{
|
||||
return $this->hasMany(Item::class);
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
//Ótima forma de adicionar um item por dentro do produto. Mas não estou usando por não saber como lidar com a table de UsuáriosPorItem
|
||||
/*
|
||||
public function addItem($name)
|
||||
{
|
||||
$this->items()->create(compact('name'));
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -27,4 +27,9 @@ class User extends Authenticatable
|
||||
protected $hidden = [
|
||||
'password', 'remember_token',
|
||||
];
|
||||
|
||||
public function products()
|
||||
{
|
||||
return $this->hasMany(Product::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user