Refactoring homeController code

This commit is contained in:
Bruno F. Fontes 2018-09-12 21:25:48 -03:00
parent 9f67cbcbeb
commit c1b4ac419d
Signed by: brunofontes
GPG Key ID: EE3447CE80048495
2 changed files with 14 additions and 14 deletions

View File

@ -23,13 +23,7 @@ class HomeController extends Controller
*/ */
public function index() public function index()
{ {
$items = \DB::table('items') $items = \App\User::find(\Auth::id())->items()->get();
->join('item_user', 'item_user.item_id', '=', 'items.id')
->join('products', 'products.id', '=', 'items.id')
->where('item_user.user_id', \Auth::id())
->select('items.*', 'products.name as pname')
->orderBy('items.name')
->get();
return view('home', compact('items')); return view('home', compact('items'));
} }
} }

View File

@ -16,23 +16,29 @@
<h4>Your itens</h4> <h4>Your itens</h4>
@forelse ($items as $item) @forelse ($items as $item)
<li> <hr class="ml-5 mr-5">
<strong>{{$item->name}}</strong> ({{$item->pname}}) <li class="mt-1 mb-1">
<strong>{{$item->name}}</strong> ({{$item->product->name}})
@if ($item->usedBy) @if ($item->usedBy)
@if ($item->usedBy == \Auth::id()) @if ($item->usedBy == \Auth::id())
<span class="float-right"> <span class="float-right">
<form action="/item/{{$item->id}}" method="POST"> <form action="/return" method="POST">
<button type="button" class="btn btn-danger" disabled>Return It</button> {{ csrf_field() }}
<input type="hidden" name="item" value="{{$item->id}}">
<button type="submit" class="btn-xm btn-danger">Return It</button>
</form> </form>
</span> </span>
<span class="float-right mr-3"><em>{{\Carbon\Carbon::parse($item->updated_at)->diffForHumans()}}</em></span>
@else @else
<div class="alert alert-danger" role="alert">In use by {{$item->usedBy}}, since {{$item->usedSince->diffForHumans()}}</div> <div class="alert alert-danger" role="alert">In use by {{$item->usedBy}}, since {{$item->updated_at->diffForHumans()}}</div>
@endif @endif
@else @else
<span class="float-right"> <span class="float-right">
<form action="/item/{{$item->id}}" method="POST"> <form action="/take" method="POST">
<button type="button" class="btn btn-success">Take It</button> {{ csrf_field() }}
<input type="hidden" name="item" value="{{$item->id}}">
<button type="submit" class="btn-xm btn-success">Take It</button>
</form> </form>
</span> </span>
@endif @endif