#Laravel Pagination with a limit on total results

12 messages · Page 1 of 1 (latest)

azure walrus
#

Hey
I've an application that needs a paginated set of results. For now I'm using

MyModel::query()->someFilters()->paginate(perPage: 30);

So I end up with a length aware paginator. It works well. Now I need to limit the amount of total results, based on some user settings.
So let's take an example. I have a total of 1000 rows. My user only has the right to see 40 results, paginated by 30. I want to do something like the code above, but so that the query cannot get paginated results over the 40th. Which means it can have a total of 2 pages: the first one with 30 results and the second one with 10 results.
However, the take and limit methods aren't usable there as being erased for the pagination (which is not a bug, it's totally normal). How can we query such a thing?

lean salmon
#

What's the use-case here? Like in what scenario would it be useful to only show a random amount of users?

azure walrus
#

It's not random. If you have a free account you cannot have all the results. When being premium we remove that limitation

lean salmon
#

I still don't understand, you can have more of those items, like an unlimited amount, but you can only "see" a subset in those results..?

azure walrus
#

We have a feature of advanced search. Let's say you search a house to buy, and we have 1000 houses recorded on our site. As a free user, we won't show you the 1000 houses. You're only allowed to see the first 40. But as soon as you become premium, you can see the 1000 results

golden fulcrum
#

Use a subquery with the limit. So whereIn('id', subquery-selecting-id-with-limit')

desert ibex
#

You can inspect the page input parameter and return an error / empty results if the user tries to access page 3, or additionally limit the returned collection on page 2

glass heron
#

Why not just pass through a query builder of sorts. Modify a given limit if it does not match your requirements.

#
public function getOrPaginate(): Collection|LengthAwarePaginator
{
    if ($this->getRawLimit() === -1) {
        return $this->builder->get();
    }

    return $this->paginate();
}

private function getLimit(): int
{
    $limit = $this->getRawLimit();

    if ($limit >= 5 && $limit <= 1000) {
        return $limit;
    }

    return static::$defaultPaginationLimit;
}
public function index(Request $request): UserCollection
{
    return UserCollection::make(
        User::filterQuery($request)->paginate()
    );
}
azure walrus
#

The solution suggested by Mono2000 seems to work really well.
I'm not sure to get what you guys mean

glass heron
#

Oh you are not just limiting the actual limit but the extent of results / last page

desert ibex