From 067cc0e0091ca85200127ea7afd00da338cc0001 Mon Sep 17 00:00:00 2001 From: Roardom Date: Sun, 26 May 2024 15:38:38 +0000 Subject: [PATCH] update: reduce queries on playlist show --- app/Http/Controllers/PlaylistController.php | 36 +++++++++++++++------ resources/views/playlist/show.blade.php | 17 +--------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/PlaylistController.php b/app/Http/Controllers/PlaylistController.php index 7c39cc51c..e622392c6 100644 --- a/app/Http/Controllers/PlaylistController.php +++ b/app/Http/Controllers/PlaylistController.php @@ -22,6 +22,7 @@ use App\Models\Movie; use App\Models\Playlist; use App\Models\Tv; use App\Repositories\ChatRepository; +use App\Traits\TorrentMeta; use Illuminate\Http\Request; use Intervention\Image\Facades\Image; use Exception; @@ -31,6 +32,8 @@ use Exception; */ class PlaylistController extends Controller { + use TorrentMeta; + /** * PlaylistController Constructor. */ @@ -93,19 +96,34 @@ class PlaylistController extends Controller { abort_if($playlist->is_private && $playlist->user_id !== $request->user()->id, 403, trans('playlist.private-error')); - $randomTorrent = $playlist->torrents()->inRandomOrder()->first(); + $randomTorrent = $playlist->torrents()->inRandomOrder()->with('category')->first(); + + $torrents = $playlist->torrents() + ->select('*') + ->selectRaw(" + 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 + ") + ->with(['category', 'resolution', 'type', 'user.group']) + ->orderBy('name') + ->paginate(26); + + // See app/Traits/TorrentMeta.php + $this->scopeMeta($torrents); return view('playlist.show', [ - 'playlist' => $playlist, - 'meta' => match(true) { - $randomTorrent?->category?->tv_meta => Tv::with('genres', 'networks', 'seasons')->find($randomTorrent->tmdb), - $randomTorrent?->category?->movie_meta => Movie::with('genres', 'companies', 'collection')->find($randomTorrent->tmdb), + 'playlist' => $playlist->load('user.group'), + 'meta' => match (true) { + $randomTorrent?->category?->tv_meta => Tv::find($randomTorrent->tmdb), + $randomTorrent?->category?->movie_meta => Movie::find($randomTorrent->tmdb), default => null, }, - 'torrents' => $playlist->torrents() - ->with(['category', 'resolution', 'type', 'user.group']) - ->orderBy('name') - ->paginate(26), + 'torrents' => $torrents, ]); } diff --git a/resources/views/playlist/show.blade.php b/resources/views/playlist/show.blade.php index 33e943aad..a1abe9ebc 100755 --- a/resources/views/playlist/show.blade.php +++ b/resources/views/playlist/show.blade.php @@ -163,23 +163,8 @@

{{ __('torrent.torrents') }}

@foreach ($torrents as $torrent) - @php - $meta = match (true) { - $torrent->category->tv_meta => App\Models\Tv::query() - ->with('genres', 'networks', 'seasons') - ->find($torrent->tmdb ?? 0), - $torrent->category->movie_meta => App\Models\Movie::query() - ->with('genres', 'companies', 'collection') - ->find($torrent->tmdb ?? 0), - $torrent->category->game_meta => MarcReichel\IGDBLaravel\Models\Game::query() - ->with(['artworks' => ['url', 'image_id'], 'genres' => ['name']]) - ->find((int) $playlistTorrent->torrent->igdb), - default => null, - }; - @endphp -
- + @if (auth()->id() === $playlist->user_id || auth()->user()->group->is_modo)