country = request()->route('country', config('app.domain_country')); } public function with(): array { return [ 'lecturers' => Lecturer::query() ->with([ 'createdBy', 'coursesEvents' => fn($query) => $query->where('from', '>=', now())->orderBy('from', 'asc'), 'coursesEvents.course', ]) ->withExists([ 'coursesEvents as has_future_events' => fn($query) => $query->where('from', '>=', now()), ]) ->withCount([ 'coursesEvents as future_events_count' => fn($query) => $query->where('from', '>=', now()), ]) ->whereHas('coursesEvents') ->whereHas('coursesEvents.venue.city.country', fn($query) => $query->where('countries.code', $this->country)) ->when($this->search, fn($query) => $query ->where('name', 'ilike', '%'.$this->search.'%') ->orWhere('description', 'ilike', '%'.$this->search.'%') ->orWhere('subtitle', 'ilike', '%'.$this->search.'%'), ) ->orderByDesc('has_future_events') ->orderBy('name', 'asc') ->paginate(15), ]; } }; ?>