Groups HOME itesm by Product

This commit is contained in:
Bruno F. Fontes 2018-09-21 00:42:19 -03:00
parent 78bc539a5a
commit b168473002
2 changed files with 27 additions and 16 deletions

View File

@ -3,7 +3,6 @@
namespace App\Http\Controllers;
use \App\User;
use Illuminate\Http\Request;
class HomeController extends Controller
{
@ -19,10 +18,10 @@ class HomeController extends Controller
/**
* Get a name from a specific id in a array
*
*
* @param array $array The array of objects with ID and Names
* @param int $id The ID of the user
*
*
* @return string The username of the specified id
*/
protected function findName($array, $id)
@ -34,6 +33,19 @@ class HomeController extends Controller
}
}
/**
* Check if the user_id alreay exists on $users array.
*
* @param array $users The array with users
* @param int $user_id The user_id to try to find on array
*
* @return boolean
*/
public function isNewUser($users, $user_id)
{
return ($user_id && !isset($users[$user_id]));
}
/**
* Show the application dashboard.
*
@ -45,14 +57,16 @@ class HomeController extends Controller
$items = User::loggedIn()->items()->with('users')->get();
foreach ($items as $item) {
if ($item->used_by && !isset($users[$item->used_by])) {
if ($this->isNewUser($users, $item->used_by)) {
$users[$item->used_by] = $this->findName($item->users, $item->used_by);
}
if ($item->waiting_user_id && !isset($users[$item->waiting_user_id])) {
if ($this->isNewUser($users, $item->waiting_user_id)) {
$users[$item->waiting_user_id] = $this->findName($item->users, $item->waiting_user_id);
}
}
return view('home', compact('items', 'users'));
$products = $items->sortBy('product.name', SORT_NATURAL | SORT_FLAG_CASE)->groupBy('product.name');
return view('home', compact('products', 'users'));
}
}

View File

@ -4,16 +4,11 @@
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Your items</div>
@forelse ($products as $items)
<div class="card mb-4">
<div class="card-header">{{$items->first()->product->name}}</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
@forelse ($items as $item)
@forelse ($items->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE) as $item)
@if (!$loop->first)
<hr class="m-3">
@endif
@ -23,7 +18,7 @@
<a href="{{$item->product->url}}" class="link-unstyled" target="_blank" rel="noopener noreferrer">
@endif
<strong>{{$item->name}}</strong> ({{$item->product->name}})
<strong>{{$item->name}}</strong>
@if ($item->product->url)
</a>
@ -42,6 +37,8 @@
@endforelse
</div>
</div>
@empty
@endforelse
</div>
</div>
</div>