Skip to content

Commit 08ced99

Browse files
authored
fix a SmartAlbum containing only portrait triggering errors (#3816)
1 parent 5229e87 commit 08ced99

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

app/Http/Resources/Traits/HasHeaderUrl.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,30 @@ private function getByQuery(AbstractAlbum $album): ?string
5454
return $header_size_variant->url;
5555
}
5656

57-
$query_ratio = SizeVariant::query()->select('photo_id');
58-
5957
/** @var Collection<int,Photo>|LengthAwarePaginator<int,Photo> $photos */
6058
$photos = $album->get_photos();
61-
if ($photos instanceof LengthAwarePaginator) {
62-
$photo_ids = collect($photos->items())->pluck('id')->all();
63-
$query_ratio = $query_ratio->whereIn('photo_id', $photo_ids);
64-
} else {
65-
$query_ratio = $query_ratio->whereBelongsTo($photos);
66-
}
67-
$query_ratio = $query_ratio->where('ratio', '>', 1)->whereIn('type', [SizeVariantType::MEDIUM, SizeVariantType::SMALL2X, SizeVariantType::SMALL]);
59+
$query_ratio = SizeVariant::query()->select('photo_id')
60+
->when($photos instanceof LengthAwarePaginator, function ($query) use ($photos): void {
61+
$photo_ids = collect($photos->items())->pluck('id')->all();
62+
$query->whereIn('photo_id', $photo_ids);
63+
})
64+
->when($photos instanceof Collection, function ($query) use ($photos): void {
65+
$query->whereBelongsTo($photos);
66+
})
67+
->where('ratio', '>', 1)->whereIn('type', [SizeVariantType::MEDIUM, SizeVariantType::SMALL2X, SizeVariantType::SMALL]);
6868
$num = $query_ratio->count() - 1;
6969
$photo = $num >= 0 ? $query_ratio->skip(rand(0, $num))->first() : null;
7070

7171
if ($photo === null) {
7272
$query = SizeVariant::query()
7373
->select('photo_id')
74-
->whereBelongsTo($album->get_photos())
74+
->when($photos instanceof LengthAwarePaginator, function ($query) use ($photos): void {
75+
$photo_ids = collect($photos->items())->pluck('id')->all();
76+
$query->whereIn('photo_id', $photo_ids);
77+
})
78+
->when($photos instanceof Collection, function ($query) use ($photos): void {
79+
$query->whereBelongsTo($photos);
80+
})
7581
->whereIn('type', [SizeVariantType::MEDIUM, SizeVariantType::SMALL2X, SizeVariantType::SMALL]);
7682
$num = $query->count() - 1;
7783
$photo = $query->skip(rand(0, $num))->first();

0 commit comments

Comments
 (0)