From 1544ee57d836a993ccc6c4d9c4a759e6832c2a39 Mon Sep 17 00:00:00 2001 From: Pedro Cunha Date: Fri, 29 Nov 2024 15:58:20 +0000 Subject: [PATCH] fix: optimize mod list query --- app/Http/Controllers/ModController.php | 9 +-------- app/Models/Mod.php | 6 ++++++ resources/views/mod/list.blade.php | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/ModController.php b/app/Http/Controllers/ModController.php index 1a26bdcc..668a67eb 100644 --- a/app/Http/Controllers/ModController.php +++ b/app/Http/Controllers/ModController.php @@ -30,14 +30,7 @@ public function getIndex(): RedirectResponse public function getList(): View { - $mods = Mod::with( - [ - 'versions' => function ($query) { - $query->orderBy('modversions.updated_at', 'desc'); - }, - ] - ) - ->get(); + $mods = Mod::with('latestVersion')->get(); return view('mod.list')->with(['mods' => $mods]); } diff --git a/app/Models/Mod.php b/app/Models/Mod.php index 7251b7fd..d3e5b1f8 100644 --- a/app/Models/Mod.php +++ b/app/Models/Mod.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasOne; class Mod extends Model { @@ -13,4 +14,9 @@ public function versions(): HasMany { return $this->hasMany(Modversion::class); } + + public function latestVersion(): HasOne + { + return $this->hasOne(Modversion::class)->latestOfMany('updated_at'); + } } diff --git a/resources/views/mod/list.blade.php b/resources/views/mod/list.blade.php index 5a33c1a7..3a46d2fa 100644 --- a/resources/views/mod/list.blade.php +++ b/resources/views/mod/list.blade.php @@ -47,7 +47,7 @@ @endif
Latest - Version: {{ !$mod->versions->isEmpty() ? $mod->versions->first()->version : "N/A" }} + Version: {{ $mod->latestVersion ? $mod->latestVersion->version : "N/A" }} {{ !empty($mod->author) ? $mod->author : "N/A" }}