You’re calling findAll() on the model, which ignores the query builder filters you’ve applied — that’s why you’re getting all records instead of the filtered count.
Straight answer:
To get the filtered count, use the same $builder you applied the filter to, and call:
$builder->countAllResults(false);
The false argument tells it not to reset the query, so you can reuse the same builder if needed.
Corrected code:
$model = new LocationModel();
$builder = $model->builder();
if ($filterName !== "" && $filterValue !== "") {
$builder->where($filterName, $filterValue);
}
$data = [];
$data['recordCount'] = $builder->countAllResults(false); // Get filtered count
$data['locations'] = $builder->get()->getResult(); // Get filtered data
Summary:
findAll() ignores any manual filters applied to $builder
Use $builder->get() to fetch filtered results
Use $builder->countAllResults(false) for filtered record count
Let me know if you’re paginating or need search/filtering across multiple fields.