Skip to content

A self-hosted media server and file management system

fix: nested eager loaded relation checks don't do anything

aminnausinbba7f7d4ec27
diff --git a/app/Http/Resources/VideoResource.php b/app/Http/Resources/VideoResource.php
index ce8b194b..ea2b3d71 100755
--- a/app/Http/Resources/VideoResource.php
+++ b/app/Http/Resources/VideoResource.php
@@ -12,16 +12,26 @@ class VideoResource extends JsonResource {
* @return array<string, mixed>
*/
public function toArray(Request $request): array {
- if (! $this->relationLoaded('metadata')) {
- $this->loadMissing('metadata');
- }
-
- if (! $this->relationLoaded('metadata.subtitles')) {
- $this->loadMissing('metadata.subtitles');
- }
+ throw_unless(
+ $this->relationLoaded('metadata'),
+ \RuntimeException::class,
+ 'VideoResource requires metadata relation to be eager-loaded.'
+ );
$metadata = $this->metadata;
+ throw_if(
+ $metadata && ! $metadata->relationLoaded('subtitles'),
+ \RuntimeException::class,
+ 'VideoResource requires metadata.subtitles relation to be eager-loaded.'
+ );
+
+ throw_if(
+ $metadata && ! $metadata->relationLoaded('videoTags'),
+ \RuntimeException::class,
+ 'VideoResource requires metadata.videoTags relation to be eager-loaded.'
+ );
+
return [
'id' => $this->id,
'name' => $this->name,
@@ -34,11 +44,11 @@ class VideoResource extends JsonResource {
'season' => $metadata?->season,
'artist' => $metadata?->artist,
'album' => $metadata?->album,
- 'intro_start' => $metadata->intro_start,
- 'intro_duration' => $metadata->intro_duration,
+ 'intro_start' => $metadata?->intro_start,
+ 'intro_duration' => $metadata?->intro_duration,
- 'video_tags' => VideoTagResource::collection($this->relationLoaded('metadata.videoTags') ? $metadata->videoTags : []),
- 'subtitles' => SubtitleResource::collection($metadata->subtitles ?? []),
+ 'video_tags' => VideoTagResource::collection($metadata?->videoTags ?? []),
+ 'subtitles' => SubtitleResource::collection($metadata?->subtitles ?? []),
'view_count' => $metadata?->view_count ?? 0,