Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

malsowayegh's avatar

Dedicated Query String Filtering: Paginate meta date

so I followed this lesson: https://laracasts.com/series/eloquent-techniques/episodes/4

for advanced query filtering.

in my FundFilter class I have this

class FundFilters extends QueryFilters
{
   
    public function requested_by($requested_by)
    {
        return $this->builder->where('requested_by', $requested_by);
    }
   
    public function closed_on($order = 'asc')
    {
        return $this->builder->orderBy('closed_on', $order);
    }
    public function paginate($value = 100)
    {
        return $this->builder->paginate($value);
    }
} 

controller:

public function index(FundFilters $filters)
    {
        return  FundResource::collection(Fund::filter($filters)->get());
    }

problem: paginate meta data doesn't appear in response

but if I did the following it get returned

note: I removed the paginate from FundFilter

public function index(FundFilters $filters)
    {
        return  FundResource::collection(Fund::filter($filters)->paginate(10));
    }

but this way I will have to parse the paginate value from the request and use it in my controller < this won't make it as clean as the query filter solution.

any idea?

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

because you have ->get() on your query.

paginate is not really a filter since it is a terminating statement, ie, when eloquent sees that statement it runs the query. You will need to handle it outside of the filters.

One approach might be to always paginate (with a very large value) and then have it overridden by the query string

    public function index(FundFilters $filters)
    {
        return  FundResource::collection(Fund::filter($filters))
                  ->paginate(request()->input('perPage',99999));
    }

where 99999 is the default value, or is overwritten by perPage=10 for instance in the querystring (adapt to your paginate count parameter)

malsowayegh's avatar

Thanks for the explanation. especially the part about terminating statement

Please or to participate in this conversation.