From d17d19bac6c7a6c1f84a3255e35f8b49ab60f8ec Mon Sep 17 00:00:00 2001 From: Rico van Zelst Date: Tue, 7 Nov 2023 22:04:00 +0100 Subject: [PATCH] fix: query optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ⚡ 166 Queries -> 2 Queries (cached) --- app/Helpers/HelperFunctions.php | 14 ++++++++++++++ app/Http/Controllers/ChampionController.php | 12 ++++++++++-- app/Models/ChampionRoles.php | 13 ------------- app/View/Components/Champions/List_all.php | 2 +- composer.json | 10 ++++++++-- resources/views/champions/index.blade.php | 2 +- .../views/components/champions/list_all.blade.php | 7 ++++--- 7 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 app/Helpers/HelperFunctions.php diff --git a/app/Helpers/HelperFunctions.php b/app/Helpers/HelperFunctions.php new file mode 100644 index 0000000..158653b --- /dev/null +++ b/app/Helpers/HelperFunctions.php @@ -0,0 +1,14 @@ + 'gm-top.png', + 'Jungle' => 'gm-jungle.png', + 'Midlane' => 'gm-mid.png', + 'Botlane' => 'gm-bot.png', + 'Support' => 'gm-support.png', + ]; + + return asset('img/' . $roleIcons[$roleName]); +} diff --git a/app/Http/Controllers/ChampionController.php b/app/Http/Controllers/ChampionController.php index 74046f7..4994d61 100644 --- a/app/Http/Controllers/ChampionController.php +++ b/app/Http/Controllers/ChampionController.php @@ -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')); } /** diff --git a/app/Models/ChampionRoles.php b/app/Models/ChampionRoles.php index f4a1423..db2bef9 100644 --- a/app/Models/ChampionRoles.php +++ b/app/Models/ChampionRoles.php @@ -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]); - } } diff --git a/app/View/Components/Champions/List_all.php b/app/View/Components/Champions/List_all.php index 9abc284..902eaa3 100644 --- a/app/View/Components/Champions/List_all.php +++ b/app/View/Components/Champions/List_all.php @@ -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) { } diff --git a/composer.json b/composer.json index 564b0f8..41996f4 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,10 @@ "name": "laravel/laravel", "type": "project", "description": "The skeleton application for the Laravel framework.", - "keywords": ["laravel", "framework"], + "keywords": [ + "laravel", + "framework" + ], "license": "MIT", "require": { "php": "^8.1", @@ -33,7 +36,10 @@ "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" - } + }, + "files": [ + "app/Helpers/HelperFunctions.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/resources/views/champions/index.blade.php b/resources/views/champions/index.blade.php index 3422de7..8ad70cc 100644 --- a/resources/views/champions/index.blade.php +++ b/resources/views/champions/index.blade.php @@ -46,7 +46,7 @@ - + diff --git a/resources/views/components/champions/list_all.blade.php b/resources/views/components/champions/list_all.blade.php index ee04dff..d667771 100644 --- a/resources/views/components/champions/list_all.blade.php +++ b/resources/views/components/champions/list_all.blade.php @@ -1,7 +1,8 @@ +/** @var App\Models\ChampionRole $roles */ +?>
@@ -36,13 +37,13 @@

- @foreach($champion->lanes->roles as $lane) + @foreach($roles[$key]->roles as $lane) {{$lane}} {{$lane}} Icon