select(['*']) ->withCount(['trackedEvents']) ->orderBy($sort, $sortData['order']); if ($sortData['search']) { $term = '%' . $sortData['search'] . '%'; $query->where(function ($query) use ($term) { $query->where('name', 'like', $term) ->orWhere('endpoint', 'like', $term); }); } return $query->paginate($count); } }