query('q', '')); $sort = $request->query('sort', 'latest'); $hasQuery = $q !== ''; $sortMap = [ 'popular' => 'views:desc', 'likes' => 'likes:desc', 'latest' => 'created_at:desc', 'downloads' => 'downloads:desc', ]; $artworks = $hasQuery ? $this->search->search($q, [ 'sort' => ($sortMap[$sort] ?? 'created_at:desc'), ]) : $this->search->popular(24); $groups = $hasQuery ? $this->groups->searchCards($q, $request->user(), 6) : $this->groups->surfaceCards($request->user(), 'featured', 4); $news = $hasQuery ? NewsArticle::query() ->with(['author:id,username,name', 'category:id,name,slug']) ->published() ->where(function ($builder) use ($q): void { $builder->where('title', 'like', '%' . $q . '%') ->orWhere('excerpt', 'like', '%' . $q . '%') ->orWhere('content', 'like', '%' . $q . '%') ->orWhere('meta_title', 'like', '%' . $q . '%'); }) ->editorialOrder() ->limit(4) ->get() : collect(); $groupResults = collect($groups ?? []); $newsResults = collect($news ?? []); $resultCount = method_exists($artworks, 'total') ? (int) $artworks->total() : 0; $groupResultCount = $groupResults->count(); $newsResultCount = $newsResults->count(); $hasAnyResults = $resultCount > 0 || $groupResultCount > 0 || $newsResultCount > 0; $galleryArtworks = collect(method_exists($artworks, 'items') ? $artworks->items() : $artworks) ->map(fn ($art) => $this->mapArtworkCard($art)) ->values(); $galleryNextPageUrl = method_exists($artworks, 'nextPageUrl') ? $artworks->nextPageUrl() : null; return view('search.index', [ 'q' => $q, 'hasQuery' => $hasQuery, 'sort' => $sort, 'groups' => $groups, 'groupResults' => $groupResults, 'groupResultCount' => $groupResultCount, 'artworks' => $artworks, 'resultCount' => $resultCount, 'news' => $news, 'newsResults' => $newsResults, 'newsResultCount' => $newsResultCount, 'hasAnyResults' => $hasAnyResults, 'galleryArtworks' => $galleryArtworks, 'galleryNextPageUrl' => $galleryNextPageUrl, 'page_title' => $hasQuery ? 'Search: ' . $q . ' — Skinbase' : 'Search — Skinbase', 'page_meta_description' => 'Search Skinbase for artworks, creators, groups, photography, wallpapers and skins.', 'page_robots' => 'noindex,follow', ]); } private function mapArtworkCard(mixed $artwork): array { return [ 'id' => $artwork->id ?? null, 'name' => $artwork->name ?? null, 'thumb' => $artwork->thumb_url ?? $artwork->thumb ?? null, 'thumb_srcset' => $artwork->thumb_srcset ?? null, 'uname' => $artwork->uname ?? '', 'username' => $artwork->username ?? '', 'avatar_url' => $artwork->avatar_url ?? null, 'profile_url' => $artwork->profile_url ?? null, 'published_as_type' => $artwork->published_as_type ?? null, 'publisher' => $artwork->publisher ?? null, 'category_name' => $artwork->category_name ?? '', 'category_slug' => $artwork->category_slug ?? '', 'slug' => $artwork->slug ?? '', 'width' => $artwork->width ?? null, 'height' => $artwork->height ?? null, 'views' => $artwork->views ?? null, 'likes' => $artwork->likes ?? null, 'downloads' => $artwork->downloads ?? null, ]; } }