Loads relationship when building download link.

This commit is contained in:
Refringe 2024-10-06 21:56:32 -06:00
parent 348e7aa532
commit 8448e54f7c
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k
2 changed files with 8 additions and 2 deletions

View File

@ -48,6 +48,8 @@ class Mod extends Model
*/ */
public function downloadUrl(bool $absolute = false): string public function downloadUrl(bool $absolute = false): string
{ {
$this->load('latestVersion');
return route('mod.version.download', [ return route('mod.version.download', [
$this->id, $this->id,
$this->slug, $this->slug,
@ -176,7 +178,7 @@ class Mod extends Model
* *
* @return HasOne<ModVersion> * @return HasOne<ModVersion>
*/ */
public function latestVersion(string $sort = 'version'): HasOne public function latestVersion(): HasOne
{ {
return $this->versions() return $this->versions()
->one() ->one()

View File

@ -35,5 +35,9 @@ it('builds download links using the latest mod version', function () {
ModVersion::factory()->recycle($mod)->create(['version' => '1.3.0']); ModVersion::factory()->recycle($mod)->create(['version' => '1.3.0']);
$modVersion = ModVersion::factory()->recycle($mod)->create(['version' => '1.3.4']); $modVersion = ModVersion::factory()->recycle($mod)->create(['version' => '1.3.4']);
expect($mod->downloadUrl())->toEqual("/mod/download/$mod->id/$mod->slug/$modVersion->version"); expect($mod->downloadUrl())->toEqual(route('mod.version.download', [
'mod' => $mod->id,
'slug' => $mod->slug,
'version' => $modVersion->version,
], absolute: false));
}); });