with('profileLink', $this->profileLink()) ->with('categories', $this->categories()) ->with('recentSearches', $this->recentSearches()) ->with('deals', $this->deals($request, $query->builder(), $addRecentSearchAction)); } protected function deals(FormRequest $request, Builder $query, AddRecentSearchAction $action): LengthAwarePaginator { // Add a search query if ($request->has('search') && $request->get('search') !== null) { $query->tap(fn($q) => (new Deal)->search($q, $request->search)); \Illuminate\Support\defer(function () use ($action, $request) { $action->execute($request->user(), ['query' => $request->search]); }); } // Add category sorting filter if ($request->has('category') && $request->get('category') !== null) { $query->tap(fn($q) => (new Deal)->filterByCategory($q, $request->category)); } // Add sorting filters if ($request->has('sortBy')) { $query = match (ExplorePageFilters::tryFrom($request->sortBy)) { ExplorePageFilters::Like => $query->orderBy('total_likes', 'desc'), ExplorePageFilters::Click => $query->orderBy('total_redirection', 'desc'), default => $query->orderByRaw( '((COALESCE(total_likes, 0) * 70.0) / 100.0) + ((COALESCE(total_redirection, 0) * 30.0) / 100.0) DESC' ) }; } return $query->latest()->paginate(); } /** * Determines the link to the user's profile dashboard * based on the user's role. * * @return string The URL for the user's dashboard. */ protected function profileLink(): string { $user = Auth::user(); return match ($user->role) { UserTypes::Broker->value => route('broker.profile.show', $user), UserTypes::User->value => route('user.profile.show', $user), default => '' }; } protected function categories(): Collection { return DealCategory::all(['id', 'name']); } protected function recentSearches(): Collection { return Auth::user()->recentSearches()->latest()->pluck('query'); } }