forge/app/View/Components/HomepageMods.php

97 lines
2.9 KiB
PHP
Raw Normal View History

2024-05-17 23:54:03 -04:00
<?php
namespace App\View\Components;
use App\Models\Mod;
use App\Models\ModVersion;
2024-05-17 23:54:03 -04:00
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
2024-05-17 23:54:03 -04:00
use Illuminate\View\Component;
class HomepageMods extends Component
2024-05-17 23:54:03 -04:00
{
public function render(): View
2024-05-17 23:54:03 -04:00
{
return view('components.homepage-mods', [
'featured' => [
'title' => __('Featured Mods'),
'mods' => $this->fetchFeaturedMods(),
'link' => '/mods?featured=only',
],
'latest' => [
'title' => __('Newest Mods'),
'mods' => $this->fetchLatestMods(),
'link' => '/mods',
],
'updated' => [
'title' => __('Recently Updated Mods'),
'mods' => $this->fetchUpdatedMods(),
'link' => '/mods?order=updated',
],
]);
2024-05-17 23:54:03 -04:00
}
2024-09-11 23:08:58 -04:00
/**
* Fetches the featured mods homepage listing.
*
* @return Collection<int, Mod>
*/
2024-05-21 21:02:49 -04:00
private function fetchFeaturedMods(): Collection
2024-05-17 23:54:03 -04:00
{
return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'downloads', 'updated_at'])
->with([
'latestVersion.latestSptVersion:id,version,color_class',
'users:id,name',
'license:id,name,link',
])
->whereFeatured(true)
->inRandomOrder()
->limit(6)
->get();
2024-05-17 23:54:03 -04:00
}
2024-09-11 23:08:58 -04:00
/**
* Fetches the latest mods homepage listing.
*
* @return Collection<int, Mod>
*/
2024-05-17 23:54:03 -04:00
private function fetchLatestMods(): Collection
{
return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'created_at', 'downloads', 'updated_at'])
->with([
'latestVersion.latestSptVersion:id,version,color_class',
'users:id,name',
'license:id,name,link',
])
->latest()
->limit(6)
->get();
2024-05-17 23:54:03 -04:00
}
2024-09-11 23:08:58 -04:00
/**
* Fetches the recently updated mods homepage listing.
*
* @return Collection<int, Mod>
*/
2024-05-17 23:54:03 -04:00
private function fetchUpdatedMods(): Collection
{
return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'downloads', 'updated_at'])
->with([
'lastUpdatedVersion.latestSptVersion:id,version,color_class',
'users:id,name',
'license:id,name,link',
])
->joinSub(
ModVersion::select('mod_id', DB::raw('MAX(updated_at) as latest_updated_at'))->groupBy('mod_id'),
'latest_versions',
'mods.id',
'=',
'latest_versions.mod_id'
)
->orderByDesc('latest_versions.latest_updated_at')
->limit(6)
->get();
2024-05-17 23:54:03 -04:00
}
}