currentRouteName = request()->route()->getName(); $this->country = request()->route('country', config('app.domain_country')); } public function with(): array { return [ 'meetups' => Meetup::with(['city.country', 'createdBy']) ->withExists([ 'meetupEvents as has_future_events' => fn($query) => $query->where('start', '>=', now()), ]) ->leftJoin('meetup_events', function ($join) { $join ->on('meetups.id', '=', 'meetup_events.meetup_id') ->where('meetup_events.start', '>=', now()); }) ->selectRaw('meetups.*, MIN(meetup_events.start) as next_event_start') ->groupBy('meetups.id') ->when($this->currentRouteName === 'meetups.index', fn($query) => $query->whereHas('city.country', fn($query) => $query->where('countries.code', $this->country)) ) ->when($this->search, fn($query) => $query->where('meetups.name', 'ilike', '%'.$this->search.'%'), ) ->orderByDesc('has_future_events') ->orderByRaw('next_event_start ASC NULLS LAST') ->paginate(15), ]; } }; ?>