fix: eager load featured torrents on home page

This commit is contained in:
Roardom
2026-03-09 07:41:03 +00:00
parent 3b51a3ed5c
commit 293463f59d
2 changed files with 47 additions and 24 deletions
+43 -4
View File
@@ -20,8 +20,11 @@ use App\Models\Article;
use App\Models\Comment;
use App\Models\FeaturedTorrent;
use App\Models\Group;
use App\Models\IgdbGame;
use App\Models\Poll;
use App\Models\Post;
use App\Models\TmdbMovie;
use App\Models\TmdbTv;
use App\Models\Topic;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
@@ -134,10 +137,46 @@ class HomeController extends Controller
'featured' => cache()->flexible(
'latest_featured',
$expiresAt,
fn () => FeaturedTorrent::query()->with([
'torrent' => ['resolution', 'type', 'category'],
'user.group',
])->get(),
static function (): \Illuminate\Database\Eloquent\Collection {
$featured = FeaturedTorrent::query()
->with([
'torrent' => fn ($query) => $query
->with(['resolution', 'type', 'category', 'user.group'])
->select('*')
->selectRaw(<<<'SQL'
CASE
WHEN category_id IN (SELECT id FROM categories WHERE movie_meta = 1) THEN 'movie'
WHEN category_id IN (SELECT id FROM categories WHERE tv_meta = 1) THEN 'tv'
WHEN category_id IN (SELECT id FROM categories WHERE game_meta = 1) THEN 'game'
WHEN category_id IN (SELECT id FROM categories WHERE music_meta = 1) THEN 'music'
WHEN category_id IN (SELECT id FROM categories WHERE no_meta = 1) THEN 'no'
END AS meta
SQL),
'user',
])
->has('torrent')
->get();
$movieIds = $featured->where('torrent.meta', '=', 'movie')->pluck('torrent.tmdb_movie_id');
$tvIds = $featured->where('torrent.meta', '=', 'tv')->pluck('torrent.tmdb_tv_id');
$gameIds = $featured->where('torrent.meta', '=', 'game')->pluck('torrent.igdb');
$movies = TmdbMovie::query()->with('genres')->whereIntegerInRaw('id', $movieIds)->get()->keyBy('id');
$tv = TmdbTv::query()->with('genres')->whereIntegerInRaw('id', $tvIds)->get()->keyBy('id');
$games = IgdbGame::query()->with('genres')->whereIntegerInRaw('id', $gameIds)->get()->keyBy('id');
return $featured->map(static function ($feature) use ($movies, $tv, $games) {
/** @phpstan-ignore property.notFound (We set this custom SQL property above) */
$feature->torrent->setAttribute('meta', match ($feature->torrent->meta) {
'movie' => $movies[$feature->torrent->tmdb_movie_id] ?? null,
'tv' => $tv[$feature->torrent->tmdb_tv_id] ?? null,
'game' => $games[$feature->torrent->tmdb_tv_id] ?? null,
default => null,
});
return $feature;
});
},
),
'poll' => cache()->flexible('latest_poll', $expiresAt, function () {
return Poll::query()->where(function ($query): void {
+4 -20
View File
@@ -21,27 +21,11 @@
<div>
<ul class="featured-carousel" x-ref="featured" x-bind="list">
@foreach ($featured as $feature)
@if ($feature->torrent === null || $feature->torrent->status !== \App\Enums\ModerationStatus::APPROVED)
@continue
@endif
@php
$meta = match (true) {
$feature->torrent->category->tv_meta => App\Models\TmdbTv::query()
->with('genres', 'networks')
->find($feature->torrent->tmdb_tv_id ?? 0),
$feature->torrent->category->movie_meta => App\Models\TmdbMovie::query()
->with('genres', 'companies')
->find($feature->torrent->tmdb_movie_id ?? 0),
$feature->torrent->category->game_meta => App\Models\Game::query()
->with('genres')
->find((int) $feature->torrent->igdb),
default => null,
};
@endphp
<li class="featured-carousel__slide">
<x-torrent.card :meta="$meta" :torrent="$feature->torrent" />
<x-torrent.card
:meta="$feature->torrent->meta"
:torrent="$feature->torrent"
/>
<footer class="featured-carousel__feature-details">
<p class="featured-carousel__featured-until">
{{ __('blocks.featured-until') }}: