mirror of
https://github.com/brunofontes/shareit.git
synced 2024-11-23 20:10:52 +00:00
Refactoring HomeController
Avoiding requests to DB
This commit is contained in:
parent
0ff176b075
commit
b40a9fa3f1
@ -17,6 +17,23 @@ class HomeController extends Controller
|
|||||||
$this->middleware('auth');
|
$this->middleware('auth');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
foreach ($array as $object) {
|
||||||
|
if ($object->id == $id) {
|
||||||
|
return $object->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show the application dashboard.
|
* Show the application dashboard.
|
||||||
*
|
*
|
||||||
@ -24,15 +41,16 @@ class HomeController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$items = User::find(\Auth::id())->items()->get();
|
$users = [];
|
||||||
|
$items = User::find(\Auth::id())->items()->with('users')->get();
|
||||||
|
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
|
if ($item->used_by && !isset($users[$item->used_by])) {
|
||||||
|
$users[$item->used_by] = $this->findName($item->users, $item->used_by);
|
||||||
|
}
|
||||||
|
|
||||||
if ($item->used_by) {
|
if ($item->waiting_user_id && !isset($users[$item->waiting_user_id])) {
|
||||||
$users[$item->used_by] = User::find($item->used_by)->name;
|
$users[$item->waiting_user_id] = $this->findName($item, $item->waiting_user_id);
|
||||||
|
|
||||||
if ($item->waiting_user_id) {
|
|
||||||
$users[$item->waiting_user_id] = User::find($item->waiting_user_id)->name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return view('home', compact('items', 'users'));
|
return view('home', compact('items', 'users'));
|
||||||
|
Loading…
Reference in New Issue
Block a user