From 4ef87f411d5e00316f9224251cc9f9b26a45479a Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Wed, 19 Sep 2018 14:58:10 -0300 Subject: [PATCH] Including authenticaed user on models: Items, Product and User --- app/Http/Controllers/AlertController.php | 4 ++-- app/Http/Controllers/HomeController.php | 2 +- app/Http/Controllers/ItemController.php | 8 ++++---- app/Http/Controllers/ProductController.php | 8 ++++---- app/Http/Controllers/TakeController.php | 4 ++-- app/Item.php | 10 ++++++++++ app/Product.php | 10 ++++++++++ app/User.php | 11 +++++++++++ 8 files changed, 44 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/AlertController.php b/app/Http/Controllers/AlertController.php index a3916b3..60dd7a7 100644 --- a/app/Http/Controllers/AlertController.php +++ b/app/Http/Controllers/AlertController.php @@ -10,7 +10,7 @@ class AlertController extends Controller { 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->timestamps = false; $item->save(); @@ -26,7 +26,7 @@ class AlertController extends Controller 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->timestamps = false; $item->save(); diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index b2f31b4..41d242c 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -42,7 +42,7 @@ class HomeController extends Controller public function index() { $users = []; - $items = User::find(\Auth::id())->items()->with('users')->get(); + $items = User::loggedIn()->items()->with('users')->get(); foreach ($items as $item) { if ($item->used_by && !isset($users[$item->used_by])) { diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index babbf9b..979c8f0 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -20,7 +20,7 @@ class ItemController extends Controller public function index() { - $items = Item::where('user_id', \Auth::id())->get(); + $items = Item::fromAuthUser()->get(); 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')]); return redirect('product/'.request('product_id')); @@ -49,7 +49,7 @@ class ItemController extends Controller public function patch(Request $request) { $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->save(); return redirect('item/'.request('item')); @@ -58,7 +58,7 @@ class ItemController extends Controller public function delete(Request $request) { $request->validate(['item' => 'required']); - $item = User::findOrFail(\Auth::id()) + $item = User::loggedIn() ->items()->with('users')->find(request('item')); $product = $item->product_id; diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 22cb29e..ba23265 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -11,7 +11,7 @@ class ProductController extends Controller { public function index() { - $products = Product::where('user_id', \Auth::id())->get(); + $products = Product::fromAuthUser()->get(); return view('product.index', compact('products')); } @@ -35,7 +35,7 @@ class ProductController extends Controller public function delete(Request $request) { $request->validate(['product' => 'required']); - $product = User::findOrFail(\Auth::id()) + $product = User::loggedIn() ->products()->with('items')->find(request('product')); //Detach users from this item @@ -51,7 +51,7 @@ class ProductController extends Controller public function patch(Request $request) { $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->url = request('url'); $product->save(); @@ -65,7 +65,7 @@ class ProductController extends Controller */ public function show($id) { - $product = Product::where('user_id', \Auth::id())->find($id); + $product = Product::fromAuthUser()->find($id); return view('product.show', compact('product')); } } diff --git a/app/Http/Controllers/TakeController.php b/app/Http/Controllers/TakeController.php index 1b62c7c..0870177 100644 --- a/app/Http/Controllers/TakeController.php +++ b/app/Http/Controllers/TakeController.php @@ -11,7 +11,7 @@ class TakeController extends Controller { 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) { return back()->withErrors("This item is already taken"); } @@ -22,7 +22,7 @@ class TakeController extends Controller 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; $item->used_by = null; $item->waiting_user_id = null; diff --git a/app/Item.php b/app/Item.php index b10da1f..5101437 100644 --- a/app/Item.php +++ b/app/Item.php @@ -28,4 +28,14 @@ class Item extends Model //Delete item $item->delete(); } + + /** + * Return the items from logged in user + * + * @return \App\Item + */ + public static function fromAuthUser() + { + return (new static)->where('user_id', \Auth::id()); + } } diff --git a/app/Product.php b/app/Product.php index db0d0e1..7e42fed 100644 --- a/app/Product.php +++ b/app/Product.php @@ -17,4 +17,14 @@ class Product extends Model { 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()); + } } diff --git a/app/User.php b/app/User.php index 55e77d6..6b353dd 100644 --- a/app/User.php +++ b/app/User.php @@ -5,6 +5,7 @@ namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; +use Illuminate\Support\Facades\Request; class User extends Authenticatable implements MustVerifyEmail { @@ -37,4 +38,14 @@ class User extends Authenticatable implements MustVerifyEmail { return $this->belongsToMany(Item::class); } + + /** + * Return the logged in user + * + * @return \App\User + */ + public static function loggedIn() + { + return (new static)->findOrFail(\Auth::id()); + } }