From 8eae71b22a556023caca8481c8ad228209a6a76b Mon Sep 17 00:00:00 2001 From: Refringe Date: Tue, 23 Jul 2024 21:21:55 -0400 Subject: [PATCH] Simplifies Latest Version Relationship This change removes the `latestSptVersion()` relationship, which fetched the latest mod version for the latest SPT version, with a more simple `latestVersion()` relationship that simply fetches the latest mod version based on the mod version, version number field. This is less complicated and much less confusing. --- app/Http/Controllers/ModController.php | 6 ++---- app/Models/Mod.php | 21 ++++++++----------- app/View/Components/ModListSection.php | 13 +++++++----- .../global-search-result-mod.blade.php | 4 ++-- resources/views/mod/show.blade.php | 14 ++++++------- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/app/Http/Controllers/ModController.php b/app/Http/Controllers/ModController.php index 40bfb0a..a315812 100644 --- a/app/Http/Controllers/ModController.php +++ b/app/Http/Controllers/ModController.php @@ -27,10 +27,8 @@ class ModController extends Controller public function show(int $modId, string $slug) { - $mod = Mod::select() - ->withTotalDownloads() - ->with(['latestSptVersion', 'users:id,name']) - ->with('license:id,name,link') + $mod = Mod::withTotalDownloads() + ->with(['latestVersion', 'users:id,name', 'license:id,name,link']) ->find($modId); if (! $mod || $mod->slug !== $slug) { diff --git a/app/Models/Mod.php b/app/Models/Mod.php index b44c52b..5c0eefb 100644 --- a/app/Models/Mod.php +++ b/app/Models/Mod.php @@ -59,7 +59,8 @@ class Mod extends Model public function lastUpdatedVersion(): HasOne { - return $this->hasOne(ModVersion::class)->orderByDesc('updated_at')->with('sptVersion'); + return $this->hasOne(ModVersion::class) + ->orderByDesc('updated_at'); } /** @@ -67,7 +68,7 @@ class Mod extends Model */ public function toSearchableArray(): array { - $latestSptVersion = $this->latestSptVersion()->first(); + $latestVersion = $this->latestVersion()->with('sptVersion')->first(); return [ 'id' => (int) $this->id, @@ -78,21 +79,17 @@ class Mod extends Model 'featured' => $this->featured, 'created_at' => strtotime($this->created_at), 'updated_at' => strtotime($this->updated_at), - 'latestSptVersion' => $latestSptVersion?->sptVersion->version, - 'latestSptVersionColorClass' => $latestSptVersion?->sptVersion->color_class, + 'latestVersion' => $latestVersion?->sptVersion->version, + 'latestVersionColorClass' => $latestVersion?->sptVersion->color_class, ]; } - public function latestSptVersion(): HasOne + /** + * The relationship to the latest mod version, dictated by the mod version number. + */ + public function latestVersion(): HasOne { return $this->hasOne(ModVersion::class) - ->orderByDesc( - SptVersion::select('version') - ->whereColumn('mod_versions.spt_version_id', 'spt_versions.id') - ->orderByDesc('version') - ->take(1), - ) - ->with('sptVersion') ->orderByDesc('version') ->take(1); } diff --git a/app/View/Components/ModListSection.php b/app/View/Components/ModListSection.php index 4805b0d..ce1c782 100644 --- a/app/View/Components/ModListSection.php +++ b/app/View/Components/ModListSection.php @@ -29,7 +29,8 @@ class ModListSection extends Component return Cache::remember('homepage-featured-mods', now()->addMinutes(5), function () { return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured']) ->withTotalDownloads() - ->with(['latestSptVersion', 'users:id,name']) + ->with(['latestVersion', 'latestVersion.sptVersion', 'users:id,name']) + ->whereHas('latestVersion') ->where('featured', true) ->latest() ->limit(6) @@ -42,7 +43,8 @@ class ModListSection extends Component return Cache::remember('homepage-latest-mods', now()->addMinutes(5), function () { return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'created_at']) ->withTotalDownloads() - ->with(['latestSptVersion', 'users:id,name']) + ->with(['latestVersion', 'latestVersion.sptVersion', 'users:id,name']) + ->whereHas('latestVersion') ->latest() ->limit(6) ->get(); @@ -54,7 +56,8 @@ class ModListSection extends Component return Cache::remember('homepage-updated-mods', now()->addMinutes(5), function () { return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured']) ->withTotalDownloads() - ->with(['lastUpdatedVersion', 'users:id,name']) + ->with(['lastUpdatedVersion', 'lastUpdatedVersion.sptVersion', 'users:id,name']) + ->whereHas('lastUpdatedVersion') ->orderByDesc( ModVersion::select('updated_at') ->whereColumn('mod_id', 'mods.id') @@ -79,12 +82,12 @@ class ModListSection extends Component [ 'title' => 'Featured Mods', 'mods' => $this->modsFeatured, - 'versionScope' => 'latestSptVersion', + 'versionScope' => 'latestVersion', ], [ 'title' => 'Newest Mods', 'mods' => $this->modsLatest, - 'versionScope' => 'latestSptVersion', + 'versionScope' => 'latestVersion', ], [ 'title' => 'Recently Updated Mods', diff --git a/resources/views/components/global-search-result-mod.blade.php b/resources/views/components/global-search-result-mod.blade.php index 9200f2d..e8dd449 100644 --- a/resources/views/components/global-search-result-mod.blade.php +++ b/resources/views/components/global-search-result-mod.blade.php @@ -6,7 +6,7 @@ {{ $result['name'] }} @endif

{{ $result['name'] }}

-

- {{ $result['latestSptVersion'] }} +

+ {{ $result['latestVersion'] }}

diff --git a/resources/views/mod/show.blade.php b/resources/views/mod/show.blade.php index 3d8012a..272f172 100644 --- a/resources/views/mod/show.blade.php +++ b/resources/views/mod/show.blade.php @@ -22,11 +22,11 @@

{{ $mod->name }} - {{ $mod->latestSptVersion->version }} + {{ $mod->latestVersion->version }}

{{ __('Created by') }} {{ $mod->users->pluck('name')->implode(', ') }}

-

{{ $mod->latestSptVersion->sptVersion->version }} {{ __('Compatible') }}

+

{{ $mod->latestVersion->sptVersion->version }} {{ __('Compatible') }}

{{ $mod->total_downloads }} {{ __('Downloads') }}

@@ -66,8 +66,8 @@
- - + +
@@ -93,12 +93,12 @@

@endif - @if($mod->latestSptVersion->virus_total_link) + @if($mod->latestVersion->virus_total_link)
  • {{ __('Latest VirusTotal Result') }}

    - - {{ $mod->latestSptVersion->virus_total_link }} + + {{ $mod->latestVersion->virus_total_link }}