mirror of
https://github.com/brunofontes/shareit.git
synced 2024-11-24 04:14:57 +00:00
Creating relationships between users/items/products
This commit is contained in:
parent
0f15e60c95
commit
a905d98304
@ -9,19 +9,17 @@ class ItemController extends Controller
|
|||||||
{
|
{
|
||||||
public function show($id)
|
public function show($id)
|
||||||
{
|
{
|
||||||
$item = \DB::table('items')
|
$item = Item::find($id);
|
||||||
->join('products', 'items.product_id', '=', 'products.id')
|
if (!$item || $item->product->user_id != \Auth::id()) return back();
|
||||||
->where([['products.admin_id', \Auth::id()], ['items.id', $id]])
|
|
||||||
->select('items.*', 'products.admin_id')
|
$otherItems = Item::where([['product_id', $item->product_id], ['id', '!=', $id]])->get();
|
||||||
->get();
|
|
||||||
$otherItems = Item::where([['product_id', $item[0]->product_id], ['id', '!=', $id]])->get();
|
|
||||||
return view('item', compact('item', 'otherItems'));
|
return view('item', compact('item', 'otherItems'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
//TODO: Fazer innerjoint com tabela de users por item
|
//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'));
|
return view('item.index', compact('items'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,6 +38,7 @@ class ItemController extends Controller
|
|||||||
'product_id' => '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
|
$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]);
|
\DB::table('item_user')->insert([ 'user_id' => \Auth::id(), 'item_id' => $id]);
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ class ProductController extends Controller
|
|||||||
{
|
{
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$products = Product::where('admin_id', \Auth::id())->get();
|
$products = Product::where('user_id', \Auth::id())->get();
|
||||||
return view('product.index', compact('products'));
|
return view('product.index', compact('products'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ class ProductController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function store()
|
public function store()
|
||||||
{
|
{
|
||||||
Product::create(['name' => request('product'), 'admin_id' => \Auth::id()]); //Just remember to add the fillable on Model to make this work
|
Product::create(['name' => request('product'), 'user_id' => \Auth::id()]); //Just remember to add the fillable on Model to make this work
|
||||||
return redirect('product');
|
return redirect('product');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ class ProductController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function show($id)
|
public function show($id)
|
||||||
{
|
{
|
||||||
$product = Product::where('admin_id', \Auth::id())->find($id);
|
$product = Product::where('user_id', \Auth::id())->find($id);
|
||||||
return view('product.show', compact('product'));
|
return view('product.show', compact('product'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,11 @@ class Item extends Model
|
|||||||
return $this->belongsTo(Product::class);
|
return $this->belongsTo(Product::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function users()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(User::class);
|
||||||
|
}
|
||||||
|
|
||||||
public function free($product_id, $user_id)
|
public function free($product_id, $user_id)
|
||||||
{
|
{
|
||||||
return $query->where([
|
return $query->where([
|
||||||
|
@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
|
|
||||||
class Product extends Model
|
class Product extends Model
|
||||||
{
|
{
|
||||||
protected $fillable = ['admin_id', 'name'];
|
protected $fillable = ['user_id', 'name'];
|
||||||
|
|
||||||
public function items()
|
public function items()
|
||||||
{
|
{
|
||||||
|
@ -32,4 +32,9 @@ class User extends Authenticatable
|
|||||||
{
|
{
|
||||||
return $this->hasMany(Product::class);
|
return $this->hasMany(Product::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function items()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Item::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ class CreateProductsTable extends Migration
|
|||||||
Schema::create('products', function (Blueprint $table) {
|
Schema::create('products', function (Blueprint $table) {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->string('name');
|
$table->string('name');
|
||||||
$table->integer('admin_id');
|
$table->integer('user_id');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -4,20 +4,9 @@
|
|||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="row justify-content-center">
|
<div class="row justify-content-center">
|
||||||
<div class="col-md-8">
|
<div class="col-md-8">
|
||||||
|
|
||||||
<form method="POST" action="/item" class="form-inline">
|
|
||||||
<div class="form-group">
|
|
||||||
{{ csrf_field() }}
|
|
||||||
<div class="col"><label for="product">Add Item: </label></div>
|
|
||||||
<div class="col-6"><input type="text" class="form-control" name="item" id="item" placeholder="One Hundred Years of Solitude" required></div>
|
|
||||||
<div class="col"><button type="submit" class="btn btn-primary">Insert</button></div>
|
|
||||||
</div>
|
|
||||||
@include ('layouts.errors')
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<div class="card mt-4">
|
<div class="card mt-4">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
Item: <strong>{{$item[0]->name}}</strong>
|
Item: <strong>{{$item->name}}</strong>
|
||||||
@include ('item.buttons')
|
@include ('item.buttons')
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
|
|
||||||
<!-- EDIT BUTTON -->
|
<!-- EDIT BUTTON -->
|
||||||
@if ($item[0]->admin_id == \Auth::id())
|
@if ($item->product->user_id == \Auth::id())
|
||||||
<form action="/item/{{$item[0]->id}}" method="POST">
|
<form action="/item/{{$item->id}}" method="POST">
|
||||||
@method('PATCH')
|
@method('PATCH')
|
||||||
<button type="button" class="btn-sm btn-secondary mr-1">Edit</button>
|
<button type="button" class="btn-sm btn-secondary mr-1">Edit</button>
|
||||||
</form>
|
</form>
|
||||||
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
|
|
||||||
<!-- DELETE BUTTON -->
|
<!-- DELETE BUTTON -->
|
||||||
<form action="/item/{{$item[0]->id}}" method="POST">
|
<form action="/item/{{$item->id}}" method="POST">
|
||||||
@method('DELETE')
|
@method('DELETE')
|
||||||
<button type="button" class="btn-sm btn-danger">Delete</button>
|
<button type="button" class="btn-sm btn-danger">Delete</button>
|
||||||
</form>
|
</form>
|
||||||
|
Loading…
Reference in New Issue
Block a user