Skip to content

Pagination

Database

Database: Pagination

Introduction

Returning an entire database table in a single response quickly becomes impractical as your application grows. Laravel's paginator works with the query builder and models with no extra setup. In a handler, r.Page reads the current page from the request's page query parameter, and the query's Paginate method fetches only the rows for that page and builds the links to the surrounding pages. You may return a paginator from an API handler as JSON, pass it to an Inertia page, or render its links in a view.

Basic usage

Paginating query results

To paginate a query, call Paginate with the request and the page you want, and give the type of each row in brackets. r.Page(15) reads the page number from the request's page query parameter and puts 15 rows on each page. Always order the query, so each row lands on the same page from one request to the next. For example, this handler returns 15 subscribers per page as JSON:

1package subscribers
2 
3import (
4 "go.laravel.com/db"
5 "go.laravel.com/web"
6)
7 
8type Subscriber struct {
9 ID int64 `json:"id"`
10 Email string `json:"email"`
11}
12 
13func Index(r *web.Request) *web.Response {
14 subscribers, err := db.Table("subscribers").OrderBy("email").Paginate[Subscriber](r, r.Page(15))
15 if err != nil {
16 return web.Fail(err)
17 }
18 
19 return web.JSON(subscribers)
20}
1package subscribers
2 
3import (
4 "go.laravel.com/db"
5 "go.laravel.com/web"
6)
7 
8type Subscriber struct {
9 ID int64 `json:"id"`
10 Email string `json:"email"`
11}
12 
13func Index(r *web.Request) *web.Response {
14 subscribers, err := db.Table("subscribers").OrderBy("email").Paginate[Subscriber](r, r.Page(15))
15 if err != nil {
16 return web.Fail(err)
17 }
18 
19 return web.JSON(subscribers)
20}

Paginate returns a *pagination.Paginator[Subscriber]. In addition to returning the paginator as JSON or passing it to a view, you may call its methods in your handler to read the current page's rows, the totals, and the navigation URLs (check out Paginator methods for the full list):

1for _, subscriber := range subscribers.Items() {
2 fmt.Println(subscriber.Email)
3}
4 
5page := subscribers.CurrentPage()
6total := subscribers.Total()
7nextURL := subscribers.NextPageURL()
1for _, subscriber := range subscribers.Items() {
2 fmt.Println(subscriber.Email)
3}
4 
5page := subscribers.CurrentPage()
6total := subscribers.Total()
7nextURL := subscribers.NextPageURL()

Because Paginate reports how many pages there are, it runs a second query that counts every matching row. If your page only shows "Previous" and "Next" links, use SimplePaginate instead. It skips the count query on large tables and returns a SimplePaginator:

1subscribers, err := db.Table("subscribers").OrderBy("id").SimplePaginate[Subscriber](r, r.Page(15))
1subscribers, err := db.Table("subscribers").OrderBy("id").SimplePaginate[Subscriber](r, r.Page(15))

Paginating models

You may also paginate a model query. Each row of a model query is already a model, so Paginate needs no brackets. For example, this handler lists published posts, newest first, using the Post model from the models guide:

1package posts
2 
3import (
4 "go.laravel.com/model"
5 "go.laravel.com/web"
6 
7 "app/models"
8)
9 
10func Index(r *web.Request) *web.Response {
11 posts, err := model.Query[models.Post]().
12 WhereNotNull("published_at").
13 Latest("published_at").
14 Paginate(r, r.Page(15))
15 if err != nil {
16 return web.Fail(err)
17 }
18 
19 return web.JSON(posts)
20}
1package posts
2 
3import (
4 "go.laravel.com/model"
5 "go.laravel.com/web"
6 
7 "app/models"
8)
9 
10func Index(r *web.Request) *web.Response {
11 posts, err := model.Query[models.Post]().
12 WhereNotNull("published_at").
13 Latest("published_at").
14 Paginate(r, r.Page(15))
15 if err != nil {
16 return web.Fail(err)
17 }
18 
19 return web.JSON(posts)
20}

Paginated model queries eager-load any relationships passed to With, as Get does. If you want to transform each model before returning the paginator, such as into a smaller response struct, call Map. It returns a new paginator with each item replaced by what your function returns, and keeps the page numbers and links:

1type PostSummary struct {
2 ID int64 `json:"id"`
3 Title string `json:"title"`
4}
5 
6summaries := posts.Map(func(post models.Post) PostSummary {
7 return PostSummary{ID: post.ID, Title: post.Title}
8})
1type PostSummary struct {
2 ID int64 `json:"id"`
3 Title string `json:"title"`
4}
5 
6summaries := posts.Map(func(post models.Post) PostSummary {
7 return PostSummary{ID: post.ID, Title: post.Title}
8})

Cursor pagination

Paginate and SimplePaginate skip rows with an SQL offset clause. On deep pages, the database still has to read and discard every skipped row, and if new records are inserted while a user is paging through results, rows shift down and may appear twice across adjacent pages.

CursorPaginate avoids both problems by encoding the ordered column values of the page's last row into an opaque cursor query parameter, so the next request resumes directly after that row. In a handler, r.Page reads that cursor from the request. CursorPaginate returns a CursorPaginator:

1posts, err := model.Query[models.Post]().
2 Latest("created_at").
3 Latest("id").
4 CursorPaginate(r, r.Page(15))
1posts, err := model.Query[models.Post]().
2 Latest("created_at").
3 Latest("id").
4 CursorPaginate(r, r.Page(15))

For example, on page 500, Paginate asks the database to read and discard 7,485 rows:

1select * from posts order by created_at desc, id desc limit 15 offset 7485
1select * from posts order by created_at desc, id desc limit 15 offset 7485

By contrast, CursorPaginate asks for the rows after the previous page's cursor, which an index on the ordered columns finds directly. It reads one extra row to learn whether another page exists:

1select * from posts
2where (created_at < ? or (created_at = ? and (id < ?)))
3order by created_at desc, id desc
4limit 16
1select * from posts
2where (created_at < ? or (created_at = ? and (id < ?)))
3order by created_at desc, id desc
4limit 16

We recommend cursor pagination for feeds, infinite scroll, and large tables. Because a cursor paginator only links to the previous and next pages, use Paginate when your page shows numbered page links.

Order by a combination of columns that uniquely identifies every row, such as a timestamp followed by the ID. A query without an order pages by id. Ordered columns can't hold NULL.

Paginator methods

Every paginator (Paginator, SimplePaginator, and CursorPaginator) provides these methods:

Method Description
Items() Returns the items on the current page.
PerPage() Returns the number of items per page.
HasMorePages() Returns whether another page follows the current page.
PreviousPageURL(), NextPageURL() Returns the URL for the previous or next page, or "" at either end.
Map(fn) Returns a new paginator with each item on the current page replaced by what fn returns.
WithQueryString(), Appends(values), WithPath(path) Returns a new paginator with customized pagination URLs.

Offset-based paginators (Paginator and SimplePaginator) also provide:

Method Description
CurrentPage() Returns the current page number, starting at 1.
URL(page) Returns the URL for a given page number.

Paginator (returned by Paginate) adds methods based on the total row count:

Method Description
Total() Returns the total number of matching rows across all pages.
LastPage() Returns the page number of the final page.
Links() Returns the page links to render, as the JSON links array holds them.
OnEachSide(count) Returns a new paginator with count page links on each side of the current page in Links.

CursorPaginator (returned by CursorPaginate) adds cursor helpers:

Method Description
PreviousCursor(), NextCursor() Returns the cursor for the previous or next page, or the zero cursor at either end.
OnFirstPage() Returns whether the current page is the first page.

Customizing pagination URLs

By default, pagination URLs start from the current request's URL and add only the page parameter, such as https://example.com/users?page=2. The request's other query parameters are left out, so navigating to page 2 drops an active search or sort order. Call WithQueryString to keep the entire query string:

1users, err := model.Query[models.User]().OrderBy("name").Paginate(r, r.Page(15))
2if err != nil {
3 return web.Fail(err)
4}
5 
6return web.JSON(users.WithQueryString())
1users, err := model.Query[models.User]().OrderBy("name").Paginate(r, r.Page(15))
2if err != nil {
3 return web.Fail(err)
4}
5 
6return web.JSON(users.WithQueryString())

WithQueryString keeps every parameter in the request, including ones your handler never reads. If you'd rather keep only the parameters your handler uses, pass them to Appends as url.Values, from the standard net/url package. Appends leaves out empty values, so when the request has no search, the page URLs have no search= parameter either. For example, this handler searches users by name and keeps the search in every page link:

1var filters struct {
2 Search string `json:"search"`
3}
4if err := r.QueryInto(&filters); err != nil {
5 return web.Fail(err)
6}
7 
8users, err := model.Query[models.User]().
9 When(filters.Search != "", func(q *model.Builder[models.User]) *model.Builder[models.User] {
10 return q.Where("name", "like", "%"+filters.Search+"%")
11 }).
12 OrderBy("name").
13 Paginate(r, r.Page(15))
14if err != nil {
15 return web.Fail(err)
16}
17 
18return web.JSON(users.Appends(url.Values{"search": {filters.Search}}))
1var filters struct {
2 Search string `json:"search"`
3}
4if err := r.QueryInto(&filters); err != nil {
5 return web.Fail(err)
6}
7 
8users, err := model.Query[models.User]().
9 When(filters.Search != "", func(q *model.Builder[models.User]) *model.Builder[models.User] {
10 return q.Where("name", "like", "%"+filters.Search+"%")
11 }).
12 OrderBy("name").
13 Paginate(r, r.Page(15))
14if err != nil {
15 return web.Fail(err)
16}
17 
18return web.JSON(users.Appends(url.Values{"search": {filters.Search}}))

To build page URLs from another path, such as https://example.com/admin/users?page=2, call WithPath:

1return web.JSON(users.WithPath("/admin/users"))
1return web.JSON(users.WithPath("/admin/users"))

WithQueryString, Appends, and WithPath each return a new paginator and leave the paginator you called them on unchanged, so return or pass on the paginator they give back.

Multiple paginators per page

When rendering multiple paginated lists on the same page, such as a dashboard with users beside posts, give each list its own query parameter with r.PageNamed. It reads the page number, or the cursor for CursorPaginate, from the parameter you name, and writes the page to that parameter in page URLs. Call WithQueryString on both paginators so paginating one list preserves the other's position:

1users, err := model.Query[models.User]().OrderBy("name").Paginate(r, r.PageNamed("users", 15))
2if err != nil {
3 return web.Fail(err)
4}
5 
6posts, err := model.Query[models.Post]().Latest("id").Paginate(r, r.PageNamed("posts", 15))
7if err != nil {
8 return web.Fail(err)
9}
10 
11return inertia.Render("Dashboard", inertia.Props{
12 "users": users.WithQueryString(),
13 "posts": posts.WithQueryString(),
14})
1users, err := model.Query[models.User]().OrderBy("name").Paginate(r, r.PageNamed("users", 15))
2if err != nil {
3 return web.Fail(err)
4}
5 
6posts, err := model.Query[models.Post]().Latest("id").Paginate(r, r.PageNamed("posts", 15))
7if err != nil {
8 return web.Fail(err)
9}
10 
11return inertia.Render("Dashboard", inertia.Props{
12 "users": users.WithQueryString(),
13 "posts": posts.WithQueryString(),
14})

Fetching a specific page

By default, r.Page reads the page number or cursor from the request's query string. When the page comes from somewhere else, such as a JSON request body, set the Number field of the pagination.Page that r.Page returns. For a cursor, parse the text with pagination.ParseCursor and set the Cursor field instead:

1type UserSearch struct {
2 Page int `json:"page"`
3}
4 
5func Search(r *web.Request) *web.Response {
6 search, err := r.Bind[UserSearch]()
7 if err != nil {
8 return web.Fail(err)
9 }
10 
11 page := r.Page(15)
12 page.Number = search.Page
13 
14 users, err := model.Query[models.User]().OrderBy("id").Paginate(r, page)
15 if err != nil {
16 return web.Fail(err)
17 }
18 
19 return web.JSON(users)
20}
1type UserSearch struct {
2 Page int `json:"page"`
3}
4 
5func Search(r *web.Request) *web.Response {
6 search, err := r.Bind[UserSearch]()
7 if err != nil {
8 return web.Fail(err)
9 }
10 
11 page := r.Page(15)
12 page.Number = search.Page
13 
14 users, err := model.Query[models.User]().OrderBy("id").Paginate(r, page)
15 if err != nil {
16 return web.Fail(err)
17 }
18 
19 return web.JSON(users)
20}

A console command or a test has no request to read, so build the pagination.Page yourself and pass the command's context. Without a URL, page URLs start from /:

1posts, err := model.Query[models.Post]().
2 OrderBy("id").
3 Paginate(ctx, pagination.Page{Number: 2, Size: 15})
1posts, err := model.Query[models.Post]().
2 OrderBy("id").
3 Paginate(ctx, pagination.Page{Number: 2, Size: 15})

Manually creating a paginator

Sometimes items come from a source outside the query builder, such as raw SQL or an external API. Fetch one page of items yourself, then pass the page's items, the total count, and the page to pagination.New. page.Offset() returns the number of rows that come before the page, for your query's offset:

1package subscribers
2 
3import (
4 "go.laravel.com/db"
5 "go.laravel.com/pagination"
6 "go.laravel.com/web"
7)
8 
9func Index(r *web.Request) *web.Response {
10 page := r.Page(15)
11 
12 subscribers, err := db.Select[Subscriber](r,
13 "select id, email from subscribers order by id limit ? offset ?", page.Size, page.Offset())
14 if err != nil {
15 return web.Fail(err)
16 }
17 
18 total, err := db.SelectOne[int](r, "select count(*) from subscribers")
19 if err != nil {
20 return web.Fail(err)
21 }
22 
23 return web.JSON(pagination.New(subscribers, total, page))
24}
1package subscribers
2 
3import (
4 "go.laravel.com/db"
5 "go.laravel.com/pagination"
6 "go.laravel.com/web"
7)
8 
9func Index(r *web.Request) *web.Response {
10 page := r.Page(15)
11 
12 subscribers, err := db.Select[Subscriber](r,
13 "select id, email from subscribers order by id limit ? offset ?", page.Size, page.Offset())
14 if err != nil {
15 return web.Fail(err)
16 }
17 
18 total, err := db.SelectOne[int](r, "select count(*) from subscribers")
19 if err != nil {
20 return web.Fail(err)
21 }
22 
23 return web.JSON(pagination.New(subscribers, total, page))
24}

Displaying pagination results

Converting results to JSON

Paginators implement json.Marshaler, encoding their items under data alongside pagination metadata and URLs. Return a paginator directly with web.JSON; page 2 of r.Page(15) encodes as follows:

1{
2 "current_page": 2,
3 "data": [{ "id": 16, "name": "Taylor" }],
4 "first_page_url": "https://example.com/users?page=1",
5 "from": 16,
6 "last_page": 3,
7 "last_page_url": "https://example.com/users?page=3",
8 "links": [
9 { "url": "https://example.com/users?page=1", "label": "&laquo; Previous", "page": 1, "active": false },
10 { "url": "https://example.com/users?page=1", "label": "1", "page": 1, "active": false },
11 { "url": "https://example.com/users?page=2", "label": "2", "page": 2, "active": true },
12 { "url": "https://example.com/users?page=3", "label": "3", "page": 3, "active": false },
13 { "url": "https://example.com/users?page=3", "label": "Next &raquo;", "page": 3, "active": false }
14 ],
15 "next_page_url": "https://example.com/users?page=3",
16 "path": "https://example.com/users",
17 "per_page": 15,
18 "prev_page_url": "https://example.com/users?page=1",
19 "to": 16,
20 "total": 45
21}
1{
2 "current_page": 2,
3 "data": [{ "id": 16, "name": "Taylor" }],
4 "first_page_url": "https://example.com/users?page=1",
5 "from": 16,
6 "last_page": 3,
7 "last_page_url": "https://example.com/users?page=3",
8 "links": [
9 { "url": "https://example.com/users?page=1", "label": "&laquo; Previous", "page": 1, "active": false },
10 { "url": "https://example.com/users?page=1", "label": "1", "page": 1, "active": false },
11 { "url": "https://example.com/users?page=2", "label": "2", "page": 2, "active": true },
12 { "url": "https://example.com/users?page=3", "label": "3", "page": 3, "active": false },
13 { "url": "https://example.com/users?page=3", "label": "Next &raquo;", "page": 3, "active": false }
14 ],
15 "next_page_url": "https://example.com/users?page=3",
16 "path": "https://example.com/users",
17 "per_page": 15,
18 "prev_page_url": "https://example.com/users?page=1",
19 "to": 16,
20 "total": 45
21}

When there are many pages, links includes the first two pages, three pages on each side of the current page, and the last two pages, inserting { "url": null, "label": "...", "active": false } entries for any omitted gaps. Call OnEachSide on the paginator to adjust how many pages appear on each side of the current page:

1return web.JSON(users.OnEachSide(1))
1return web.JSON(users.OnEachSide(1))

For example, on page 10 of 20 with OnEachSide(1), the links array holds entries for 1, 2, ..., 9, 10, 11, ..., 19, and 20:

1[
2 { "url": "https://example.com/users?page=9", "label": "&laquo; Previous", "page": 9, "active": false },
3 { "url": "https://example.com/users?page=1", "label": "1", "page": 1, "active": false },
4 { "url": "https://example.com/users?page=2", "label": "2", "page": 2, "active": false },
5 { "url": null, "label": "...", "active": false },
6 { "url": "https://example.com/users?page=9", "label": "9", "page": 9, "active": false },
7 { "url": "https://example.com/users?page=10", "label": "10", "page": 10, "active": true },
8 { "url": "https://example.com/users?page=11", "label": "11", "page": 11, "active": false },
9 { "url": null, "label": "...", "active": false },
10 { "url": "https://example.com/users?page=19", "label": "19", "page": 19, "active": false },
11 { "url": "https://example.com/users?page=20", "label": "20", "page": 20, "active": false },
12 { "url": "https://example.com/users?page=11", "label": "Next &raquo;", "page": 11, "active": false }
13]
1[
2 { "url": "https://example.com/users?page=9", "label": "&laquo; Previous", "page": 9, "active": false },
3 { "url": "https://example.com/users?page=1", "label": "1", "page": 1, "active": false },
4 { "url": "https://example.com/users?page=2", "label": "2", "page": 2, "active": false },
5 { "url": null, "label": "...", "active": false },
6 { "url": "https://example.com/users?page=9", "label": "9", "page": 9, "active": false },
7 { "url": "https://example.com/users?page=10", "label": "10", "page": 10, "active": true },
8 { "url": "https://example.com/users?page=11", "label": "11", "page": 11, "active": false },
9 { "url": null, "label": "...", "active": false },
10 { "url": "https://example.com/users?page=19", "label": "19", "page": 19, "active": false },
11 { "url": "https://example.com/users?page=20", "label": "20", "page": 20, "active": false },
12 { "url": "https://example.com/users?page=11", "label": "Next &raquo;", "page": 11, "active": false }
13]

SimplePaginator omits last_page, last_page_url, links, and total from its JSON output, and adds current_page_url. CursorPaginator encodes data, path, per_page, next_cursor, next_page_url, prev_cursor, and prev_page_url.

In a view, Laravel includes a pagination::links view that renders a paginator's links for you. Pass the paginator as the view's data, then include pagination::links where the links belong. It renders numbered links styled with Tailwind CSS, with Previous and Next at either end, and renders nothing when every item fits on one page:

1return web.View("posts.index", posts)
1return web.View("posts.index", posts)
1<!-- resources/views/posts/index.html -->
2 
3{{range .Items}}
4 <article>{{.Title}}</article>
5{{end}}
6 
7{{template "pagination::links" .}}
1<!-- resources/views/posts/index.html -->
2 
3{{range .Items}}
4 <article>{{.Title}}</article>
5{{end}}
6 
7{{template "pagination::links" .}}

A paginator from SimplePaginate or CursorPaginate doesn't count its pages, so it can't render numbered links. Include pagination::simple instead, which renders only Previous and Next and works with every kind of paginator:

1{{template "pagination::simple" .}}
1{{template "pagination::simple" .}}

Tailwind CSS generates only the classes it finds in your own files, so resources/css/app.css in new applications names the classes that these links use. If your application's stylesheet doesn't have this line, add it, or the links render unstyled:

1@source inline("flex items-center justify-between gap-1 rounded-md border border-gray-200 border-gray-300 border-gray-900 bg-gray-900 hover:bg-gray-50 px-3 py-1.5 text-sm text-white text-gray-400 text-gray-700");
1@source inline("flex items-center justify-between gap-1 rounded-md border border-gray-200 border-gray-300 border-gray-900 bg-gray-900 hover:bg-gray-50 px-3 py-1.5 text-sm text-white text-gray-400 text-gray-700");

If you'd rather write the markup yourself, loop over the paginator's .Links. Each link has a URL, a Label, a Page number, and an Active flag that marks the current page. The labels are plain text, such as « Previous, 2, and Next », and a gap's label is ...:

1<nav>
2 {{range .Links}}
3 {{if .URL}}
4 <a href="{{.URL}}" {{if .Active}}aria-current="page"{{end}}>{{.Label}}</a>
5 {{else}}
6 <span>{{.Label}}</span>
7 {{end}}
8 {{end}}
9</nav>
1<nav>
2 {{range .Links}}
3 {{if .URL}}
4 <a href="{{.URL}}" {{if .Active}}aria-current="page"{{end}}>{{.Label}}</a>
5 {{else}}
6 <span>{{.Label}}</span>
7 {{end}}
8 {{end}}
9</nav>

To restyle the links that pagination::links and pagination::simple render across your application, copy Laravel's pagination views into your application with vendor:publish, then edit resources/views/vendor/pagination/link.html:

1laravel vendor:publish pagination
1laravel vendor:publish pagination

Pass the paginator as a prop, such as inertia.Props{"users": users}, and render its links with Inertia's Link component. The "Previous" and "Next" labels contain HTML entities, so render labels as HTML:

1import { Link } from '@inertiajs/react';
2 
3export default function Index({ users }) {
4 return (
5 <nav>
6 {users.links.map((link, index) =>
7 link.url ? (
8 <Link
9 key={index}
10 href={link.url}
11 className={link.active ? 'font-bold' : ''}
12 dangerouslySetInnerHTML={{ __html: link.label }}
13 />
14 ) : (
15 <span key={index} dangerouslySetInnerHTML={{ __html: link.label }} />
16 ),
17 )}
18 </nav>
19 );
20}
1import { Link } from '@inertiajs/react';
2 
3export default function Index({ users }) {
4 return (
5 <nav>
6 {users.links.map((link, index) =>
7 link.url ? (
8 <Link
9 key={index}
10 href={link.url}
11 className={link.active ? 'font-bold' : ''}
12 dangerouslySetInnerHTML={{ __html: link.label }}
13 />
14 ) : (
15 <span key={index} dangerouslySetInnerHTML={{ __html: link.label }} />
16 ),
17 )}
18 </nav>
19 );
20}

If you render pagination links on multiple pages, extract this into a reusable component (such as <Pagination links={users.links} />) so your pagination markup and styling stay in one place.

Infinite scroll

A feed often reads better as one long list that grows as the user scrolls than as numbered pages. Inertia's <InfiniteScroll> component loads the next page when the user nears the end of the list. Pass inertia.Scroll a function that fetches the page:

1package posts
2 
3import (
4 "context"
5 
6 "go.laravel.com/inertia"
7 "go.laravel.com/model"
8 "go.laravel.com/web"
9 
10 "app/models"
11)
12 
13func Index(r *web.Request) *web.Response {
14 return inertia.Render("Posts/Index", inertia.Props{
15 "posts": inertia.Scroll(func(ctx context.Context) (any, error) {
16 return model.Query[models.Post]().Latest("id").CursorPaginate(ctx, r.Page(15))
17 }),
18 "authors": inertia.Lazy(model.Query[models.User]().OrderBy("name").Get),
19 })
20}
1package posts
2 
3import (
4 "context"
5 
6 "go.laravel.com/inertia"
7 "go.laravel.com/model"
8 "go.laravel.com/web"
9 
10 "app/models"
11)
12 
13func Index(r *web.Request) *web.Response {
14 return inertia.Render("Posts/Index", inertia.Props{
15 "posts": inertia.Scroll(func(ctx context.Context) (any, error) {
16 return model.Query[models.Post]().Latest("id").CursorPaginate(ctx, r.Page(15))
17 }),
18 "authors": inertia.Lazy(model.Query[models.User]().OrderBy("name").Get),
19 })
20}

To load the next page, the component requests only the posts prop. Because Inertia runs a prop's function only when the response includes that prop, scroll requests run the posts query while skipping the authors query.

inertia.Scroll(posts) also takes a paginator you already fetched. Because the handler then runs every query before Inertia sees which props the request asked for, prefer passing a function whenever the page includes other props.

On the page, wrap the items in <InfiniteScroll> with data set to the prop's name, and optionally pass a loading prop to render an indicator while the next page is being fetched:

1import { InfiniteScroll } from '@inertiajs/react';
2 
3export default function Index({ posts }) {
4 return (
5 <InfiniteScroll data="posts" loading={() => <div>Loading more posts...</div>}>
6 {posts.data.map((post) => (
7 <article key={post.id}>{post.title}</article>
8 ))}
9 </InfiniteScroll>
10 );
11}
1import { InfiniteScroll } from '@inertiajs/react';
2 
3export default function Index({ posts }) {
4 return (
5 <InfiniteScroll data="posts" loading={() => <div>Loading more posts...</div>}>
6 {posts.data.map((post) => (
7 <article key={post.id}>{post.title}</article>
8 ))}
9 </InfiniteScroll>
10 );
11}

If you prefer a "Load more" button instead of automatic scrolling, add the manual prop and pass a next render prop receiving loading, fetch, and hasMore:

1import { InfiniteScroll } from '@inertiajs/react';
2 
3export default function Index({ posts }) {
4 return (
5 <InfiniteScroll
6 data="posts"
7 manual
8 next={({ loading, fetch, hasMore }) =>
9 hasMore ? (
10 <button onClick={fetch} disabled={loading}>
11 {loading ? 'Loading...' : 'Load more'}
12 </button>
13 ) : null
14 }
15 >
16 {posts.data.map((post) => (
17 <article key={post.id}>{post.title}</article>
18 ))}
19 </InfiniteScroll>
20 );
21}
1import { InfiniteScroll } from '@inertiajs/react';
2 
3export default function Index({ posts }) {
4 return (
5 <InfiniteScroll
6 data="posts"
7 manual
8 next={({ loading, fetch, hasMore }) =>
9 hasMore ? (
10 <button onClick={fetch} disabled={loading}>
11 {loading ? 'Loading...' : 'Load more'}
12 </button>
13 ) : null
14 }
15 >
16 {posts.data.map((post) => (
17 <article key={post.id}>{post.title}</article>
18 ))}
19 </InfiniteScroll>
20 );
21}

For additional options such as reverse scrolling, scroll buffers, and table layouts, check out Inertia's infinite scroll documentation.