diff --git a/app/Http/Controllers/MediaController.php b/app/Http/Controllers/MediaController.php index e6c86d6..4b5a498 100644 --- a/app/Http/Controllers/MediaController.php +++ b/app/Http/Controllers/MediaController.php @@ -482,10 +482,14 @@ private function similarMedia(Media $media, int $limit = 5) 'tags as tag_matches' => fn ($t) => $t->whereIn('tags.id', $tagIds ?: [0]), ]) ->with(['image', 'detailImage', 'categories', 'subCategories', 'tags']) - ->orderByRaw('(category_matches + subcategory_matches + tag_matches) desc') + // Pull a recent candidate pool (portable; Postgres can't ORDER BY alias + // expressions), then rank by overlap in PHP. ->orderByDesc('created_at') - ->limit($limit) - ->get(); + ->limit(60) + ->get() + ->sortByDesc(fn ($m) => $m->category_matches + $m->subcategory_matches + $m->tag_matches) + ->take($limit) + ->values(); } public function rate(Request $request, $mediaId)