fix: query optimization

 166 Queries -> 2 Queries (cached)
This commit is contained in:
Rico van Zelst
2023-11-07 22:04:00 +01:00
parent 04602efece
commit d17d19bac6
7 changed files with 38 additions and 22 deletions

View File

@@ -0,0 +1,14 @@
<?php
function getRoleIcon($roleName)
{
$roleIcons = [
'Toplane' => 'gm-top.png',
'Jungle' => 'gm-jungle.png',
'Midlane' => 'gm-mid.png',
'Botlane' => 'gm-bot.png',
'Support' => 'gm-support.png',
];
return asset('img/' . $roleIcons[$roleName]);
}

View File

@@ -5,6 +5,8 @@ namespace App\Http\Controllers;
use App\Http\Requests\StoreChampionRequest;
use App\Http\Requests\UpdateChampionRequest;
use App\Models\Champion;
use App\Models\ChampionRoles;
use Illuminate\Support\Facades\Cache;
class ChampionController extends Controller
{
@@ -13,9 +15,15 @@ class ChampionController extends Controller
*/
public function index()
{
$champions = Champion::orderBy('name')->get();
$champions = Cache::remember('championsListAllCache', 60 * 60 * 8, function () {
return Champion::orderBy('name')->get();
});
return view('champions.index', compact('champions'));
$roles = Cache::remember('championsRolesCache', 60 * 60 * 8, function () {
return ChampionRoles::orderBy('champion_name')->get();
});
return view('champions.index', compact('champions', 'roles'));
}
/**

View File

@@ -47,17 +47,4 @@ class ChampionRoles extends Model
return $transformedRoles;
}
public function getRoleIcon($roleName)
{
$roleIcons = [
'Toplane' => 'gm-top.png',
'Jungle' => 'gm-jungle.png',
'Midlane' => 'gm-mid.png',
'Botlane' => 'gm-bot.png',
'Support' => 'gm-support.png',
];
return asset('img/' . $roleIcons[$roleName]);
}
}

View File

@@ -7,7 +7,7 @@ use Illuminate\View\Component;
class List_all extends Component
{
public function __construct(public array $champions)
public function __construct(public array $champions, public array $roles)
{
}