Including authenticaed user on models: Items, Product and User

This commit is contained in:
Bruno F. Fontes 2018-09-19 14:58:10 -03:00
parent 3a395f7069
commit 4ef87f411d
8 changed files with 44 additions and 13 deletions

View File

@ -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();

View File

@ -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])) {

View File

@ -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;

View File

@ -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'));
}
}

View File

@ -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;

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}