Skip to content

Commit

Permalink
fix: optimize mod list query
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyker committed Nov 29, 2024
1 parent dc4b4d6 commit 1544ee5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
9 changes: 1 addition & 8 deletions app/Http/Controllers/ModController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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');
}
}
2 changes: 1 addition & 1 deletion resources/views/mod/list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
@endif
<br>
<b>Latest
Version:</b> {{ !$mod->versions->isEmpty() ? $mod->versions->first()->version : "N/A" }}
Version:</b> {{ $mod->latestVersion ? $mod->latestVersion->version : "N/A" }}
</td>
<td>{{ !empty($mod->author) ? $mod->author : "N/A" }}</td>
<td>
Expand Down

0 comments on commit 1544ee5

Please sign in to comment.