---
title: "Pagination — Query Builder for Laravel | Roundly"
description: "Validate and hard-cap per_page with the HasPageSize FormRequest trait; the configured page name is applied automatically."
url: https://roundly-consulting.com/open-source/docs/query-builder-for-laravel/pagination
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Query Builder for Laravel](https://roundly-consulting.com/open-source/docs/query-builder-for-laravel.md)

# Pagination

Mix HasPageSize into a FormRequest to validate and resolve the page size:

```php
use Illuminate\Foundation\Http\FormRequest;
use RoundlyConsulting\QueryBuilder\Concerns\HasPageSize;

final class ListPostsRequest extends FormRequest
{
    use HasPageSize;

    public function rules(): array
    {
        return [
            ...$this->pageSizeRules(),
            // your other rules
        ];
    }
}
```

```php
$posts = QueryBuilder::for(Post::class)
    ->allowedFilters('status')
    ->paginate($request->perPage());
```

## What the trait gives you

| Method | Returns |
| --- | --- |
| `pageSizeRules()` | \['per\_page' => \['nullable', 'integer', 'min:1', 'max:100'\]\] — merge into rules(). |
| `perPage()` | The requested size, hard-capped at max\_per\_page; default\_per\_page when absent or invalid. |
| `pageName()` | The configured pagination.page\_name. |

per\_page above max\_per\_page, below 1, or non-integer yields a 422. perPage() also hard-caps at max\_per\_page and falls back to default\_per\_page as defence in depth.

## Page parameter name

paginate() and simplePaginate() on a QueryBuilder are handed the configured pagination.page\_name automatically, so renaming it in config is honoured on the wire and in the generated links. Pass pageName: yourself to override it, or read it from the request when you build a paginator by hand:

```php
// config/query-builder.php
'pagination' => [
    'page_name' => 'p',
    // ...
],

// QueryBuilder applies it: ?p=2, and the paginator links use ?p=
QueryBuilder::for(Post::class)->paginate($request->perPage());

// A paginator you build yourself
Post::query()->paginate($request->perPage(), pageName: $request->pageName());
```

A page name passed explicitly — as pageName: or the third positional argument — always wins. Cursor pagination is out of scope.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/query-builder-for-laravel.md)
