forge/app/View/Components/HomepageMods.php

91 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2024-05-17 23:54:03 -04:00
<?php
2025-01-30 00:23:55 -05:00
declare(strict_types=1);
2024-05-17 23:54:03 -04:00
namespace App\View\Components;
use App\Models\Mod;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
2024-09-15 23:27:00 -04:00
use Illuminate\Support\Facades\Cache;
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.
2025-01-30 20:49:56 -05:00
*
* @return Collection<int, Mod>
2024-09-11 23:08:58 -04:00
*/
2024-05-21 21:02:49 -04:00
private function fetchFeaturedMods(): Collection
2024-05-17 23:54:03 -04:00
{
2025-01-30 00:23:55 -05:00
return Cache::flexible('homepage-featured-mods', [5, 10], fn () => Mod::whereFeatured(true)
->with([
'latestVersion',
'latestVersion.latestSptVersion',
'users:id,name',
'license:id,name,link',
])
->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.
2025-01-30 20:49:56 -05:00
*
* @return Collection<int, Mod>
2024-09-11 23:08:58 -04:00
*/
2024-05-17 23:54:03 -04:00
private function fetchLatestMods(): Collection
{
2025-01-30 00:50:28 -05:00
return Cache::flexible('homepage-latest-mods', [5, 10], fn () => Mod::query()->orderByDesc('created_at')
2025-01-30 00:23:55 -05:00
->with([
'latestVersion',
'latestVersion.latestSptVersion',
'users:id,name',
'license:id,name,link',
])
->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.
2025-01-30 20:49:56 -05:00
*
* @return Collection<int, Mod>
2024-09-11 23:08:58 -04:00
*/
2024-05-17 23:54:03 -04:00
private function fetchUpdatedMods(): Collection
{
2025-01-30 00:50:28 -05:00
return Cache::flexible('homepage-updated-mods', [5, 10], fn () => Mod::query()->orderByDesc('updated_at')
2025-01-30 00:23:55 -05:00
->with([
'latestUpdatedVersion',
'latestUpdatedVersion.latestSptVersion',
'users:id,name',
'license:id,name,link',
])
->limit(6)
->get());
2024-05-17 23:54:03 -04:00
}
}