shareit/app/Http/Controllers/HomeController.php
Bruno Fontes 999157825f Fixed bug causing error 500 on production
Some PHP version or configuration were causing this error.

On app.blade.php of local branch, I could use "$usedItems ? :"
even if $usedItems were null, but I had to check an "isset" to the production.

On HomecController, I had to change the "object" parameter of getUsername
to "\Illuminate\Database\Eloquent\Collection" to make it work.

I took the chance to just show the number of itens in use
if it were greater than 0.
2018-10-16 20:28:59 -03:00

82 lines
2.0 KiB
PHP

<?php
namespace App\Http\Controllers;
use \App\User;
class HomeController extends Controller
{
protected $activeUsers = [];
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$items = User::loggedIn()->items()->with('users')->get();
$numberOfUsedItems = 0;
foreach ($items as $item) {
if (isset($item->used_by)) {
$numberOfUsedItems++;
}
$this->getUsername($item->users, $item->used_by);
$this->getUsername($item->users, $item->waiting_user_id);
}
$products = $items
->sortBy('product.name', SORT_NATURAL | SORT_FLAG_CASE)
->groupBy('product.name');
return view(
'home',
['products' => $products, 'users' => $this->activeUsers, 'usedItems' => $numberOfUsedItems]
);
}
/**
* Get the username from an specified user id.
*
* @param object $itemUsers Array with IDs and usernames
* @param int $id The user id to search for
*
* @return void
*/
protected function getUsername(\Illuminate\Database\Eloquent\Collection $itemUsers, ?int $id)
{
if ($id && !isset($this->activeUsers[$id])) {
$this->activeUsers[$id] = $this->findName($itemUsers, $id);
}
}
/**
* 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;
}
}
}
}