mirror of
https://github.com/brunofontes/shareit.git
synced 2024-11-24 04:14:57 +00:00
Including authenticaed user on models: Items, Product and User
This commit is contained in:
parent
3a395f7069
commit
4ef87f411d
@ -10,7 +10,7 @@ class AlertController extends Controller
|
|||||||
{
|
{
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$item = User::find(\Auth::id())->items()->find(request('item'));
|
$item = User::loggedIn()->items()->find(request('item'));
|
||||||
$item->waiting_user_id = \Auth::id();
|
$item->waiting_user_id = \Auth::id();
|
||||||
$item->timestamps = false;
|
$item->timestamps = false;
|
||||||
$item->save();
|
$item->save();
|
||||||
@ -26,7 +26,7 @@ class AlertController extends Controller
|
|||||||
|
|
||||||
public function delete(Request $request)
|
public function delete(Request $request)
|
||||||
{
|
{
|
||||||
$item = User::find(\Auth::id())->items()->find(request('item'));
|
$item = User::loggedIn()->items()->find(request('item'));
|
||||||
$item->waiting_user_id = null;
|
$item->waiting_user_id = null;
|
||||||
$item->timestamps = false;
|
$item->timestamps = false;
|
||||||
$item->save();
|
$item->save();
|
||||||
|
@ -42,7 +42,7 @@ class HomeController extends Controller
|
|||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$users = [];
|
$users = [];
|
||||||
$items = User::find(\Auth::id())->items()->with('users')->get();
|
$items = User::loggedIn()->items()->with('users')->get();
|
||||||
|
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
if ($item->used_by && !isset($users[$item->used_by])) {
|
if ($item->used_by && !isset($users[$item->used_by])) {
|
||||||
|
@ -20,7 +20,7 @@ class ItemController extends Controller
|
|||||||
|
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$items = Item::where('user_id', \Auth::id())->get();
|
$items = Item::fromAuthUser()->get();
|
||||||
return view('item.index', compact('items'));
|
return view('item.index', compact('items'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ class ItemController extends Controller
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
$authUser = User::find(\Auth::id());
|
$authUser = User::loggedIn();
|
||||||
$authUser->items()->create(['name' => request('item'), 'product_id' => request('product_id')]);
|
$authUser->items()->create(['name' => request('item'), 'product_id' => request('product_id')]);
|
||||||
|
|
||||||
return redirect('product/'.request('product_id'));
|
return redirect('product/'.request('product_id'));
|
||||||
@ -49,7 +49,7 @@ class ItemController extends Controller
|
|||||||
public function patch(Request $request)
|
public function patch(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate(['item' => 'required', 'name' => 'required']);
|
$request->validate(['item' => 'required', 'name' => 'required']);
|
||||||
$item = User::findOrFail(\Auth::id())->items()->find(request('item'));
|
$item = User::loggedIn()->items()->find(request('item'));
|
||||||
$item->name = request('name');
|
$item->name = request('name');
|
||||||
$item->save();
|
$item->save();
|
||||||
return redirect('item/'.request('item'));
|
return redirect('item/'.request('item'));
|
||||||
@ -58,7 +58,7 @@ class ItemController extends Controller
|
|||||||
public function delete(Request $request)
|
public function delete(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate(['item' => 'required']);
|
$request->validate(['item' => 'required']);
|
||||||
$item = User::findOrFail(\Auth::id())
|
$item = User::loggedIn()
|
||||||
->items()->with('users')->find(request('item'));
|
->items()->with('users')->find(request('item'));
|
||||||
$product = $item->product_id;
|
$product = $item->product_id;
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ class ProductController extends Controller
|
|||||||
{
|
{
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$products = Product::where('user_id', \Auth::id())->get();
|
$products = Product::fromAuthUser()->get();
|
||||||
return view('product.index', compact('products'));
|
return view('product.index', compact('products'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ class ProductController extends Controller
|
|||||||
public function delete(Request $request)
|
public function delete(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate(['product' => 'required']);
|
$request->validate(['product' => 'required']);
|
||||||
$product = User::findOrFail(\Auth::id())
|
$product = User::loggedIn()
|
||||||
->products()->with('items')->find(request('product'));
|
->products()->with('items')->find(request('product'));
|
||||||
|
|
||||||
//Detach users from this item
|
//Detach users from this item
|
||||||
@ -51,7 +51,7 @@ class ProductController extends Controller
|
|||||||
public function patch(Request $request)
|
public function patch(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate(['product' => 'required', 'name' => 'required']);
|
$request->validate(['product' => 'required', 'name' => 'required']);
|
||||||
$product = User::find(\Auth::id())->products()->find(request('product'));
|
$product = User::loggedIn()->products()->find(request('product'));
|
||||||
$product->name = request('name');
|
$product->name = request('name');
|
||||||
$product->url = request('url');
|
$product->url = request('url');
|
||||||
$product->save();
|
$product->save();
|
||||||
@ -65,7 +65,7 @@ class ProductController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function show($id)
|
public function show($id)
|
||||||
{
|
{
|
||||||
$product = Product::where('user_id', \Auth::id())->find($id);
|
$product = Product::fromAuthUser()->find($id);
|
||||||
return view('product.show', compact('product'));
|
return view('product.show', compact('product'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ class TakeController extends Controller
|
|||||||
{
|
{
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$item = User::find(\Auth::id())->items()->find(request('item'));
|
$item = User::loggedIn()->items()->find(request('item'));
|
||||||
if ($item->used_by) {
|
if ($item->used_by) {
|
||||||
return back()->withErrors("This item is already taken");
|
return back()->withErrors("This item is already taken");
|
||||||
}
|
}
|
||||||
@ -22,7 +22,7 @@ class TakeController extends Controller
|
|||||||
|
|
||||||
public function delete(Request $request)
|
public function delete(Request $request)
|
||||||
{
|
{
|
||||||
$item = User::find(\Auth::id())->items()->find(request('item'));
|
$item = User::loggedIn()->items()->find(request('item'));
|
||||||
$waiting_id = $item->waiting_user_id;
|
$waiting_id = $item->waiting_user_id;
|
||||||
$item->used_by = null;
|
$item->used_by = null;
|
||||||
$item->waiting_user_id = null;
|
$item->waiting_user_id = null;
|
||||||
|
10
app/Item.php
10
app/Item.php
@ -28,4 +28,14 @@ class Item extends Model
|
|||||||
//Delete item
|
//Delete item
|
||||||
$item->delete();
|
$item->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the items from logged in user
|
||||||
|
*
|
||||||
|
* @return \App\Item
|
||||||
|
*/
|
||||||
|
public static function fromAuthUser()
|
||||||
|
{
|
||||||
|
return (new static)->where('user_id', \Auth::id());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,4 +17,14 @@ class Product extends Model
|
|||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the products from logged in user
|
||||||
|
*
|
||||||
|
* @return \App\Product
|
||||||
|
*/
|
||||||
|
public static function fromAuthUser()
|
||||||
|
{
|
||||||
|
return (new static)->where('user_id', \Auth::id());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
11
app/User.php
11
app/User.php
@ -5,6 +5,7 @@ namespace App;
|
|||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
|
use Illuminate\Support\Facades\Request;
|
||||||
|
|
||||||
class User extends Authenticatable implements MustVerifyEmail
|
class User extends Authenticatable implements MustVerifyEmail
|
||||||
{
|
{
|
||||||
@ -37,4 +38,14 @@ class User extends Authenticatable implements MustVerifyEmail
|
|||||||
{
|
{
|
||||||
return $this->belongsToMany(Item::class);
|
return $this->belongsToMany(Item::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the logged in user
|
||||||
|
*
|
||||||
|
* @return \App\User
|
||||||
|
*/
|
||||||
|
public static function loggedIn()
|
||||||
|
{
|
||||||
|
return (new static)->findOrFail(\Auth::id());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user