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 integrates directly with the query builder and models with no extra setup. It reads the current page from the request's page query parameter, fetches only the rows for that page, and builds the links to the surrounding pages. You may return a paginator directly from an API handler, pass it to an Inertia page, or render its links inside a view.

Basic usage

Paginating query results

To paginate a query, call paginate with the number of items you want per page. Laravel calculates the limit and offset from the request's page query parameter. Always include an explicit order_by clause so rows stay on consistent pages across requests. For example, this handler returns 15 subscribers per page as JSON:

1use laravel::db;
2use laravel::pagination::Paginator;
3use laravel::prelude::*;
4use serde::Serialize;
5 
6#[derive(db::FromRow, Serialize)]
7pub struct Subscriber {
8 id: i64,
9 email: String,
10}
11 
12pub async fn index() -> laravel::Result<Json<Paginator<Subscriber>>> {
13 let subscribers = db::table("subscribers")
14 .order_by("email")
15 .paginate::<Subscriber>(15)
16 .await?;
17 
18 Ok(Json(subscribers))
19}
1use laravel::db;
2use laravel::pagination::Paginator;
3use laravel::prelude::*;
4use serde::Serialize;
5 
6#[derive(db::FromRow, Serialize)]
7pub struct Subscriber {
8 id: i64,
9 email: String,
10}
11 
12pub async fn index() -> laravel::Result<Json<Paginator<Subscriber>>> {
13 let subscribers = db::table("subscribers")
14 .order_by("email")
15 .paginate::<Subscriber>(15)
16 .await?;
17 
18 Ok(Json(subscribers))
19}

In addition to returning a Paginator as JSON or passing it to a view, you may inspect the paginator directly in Rust to access the current page's records, totals, and navigation URLs (check out Paginator methods for the full list):

1let subscribers = db::table("subscribers").order_by("email").paginate::<Subscriber>(15).await?;
2 
3for subscriber in subscribers.items() {
4 println!("{}", subscriber.email);
5}
6 
7let page = subscribers.current_page();
8let total = subscribers.total();
9let next_url = subscribers.next_page_url();
1let subscribers = db::table("subscribers").order_by("email").paginate::<Subscriber>(15).await?;
2 
3for subscriber in subscribers.items() {
4 println!("{}", subscriber.email);
5}
6 
7let page = subscribers.current_page();
8let total = subscribers.total();
9let next_url = subscribers.next_page_url();

Because paginate computes total page counts, it executes a second query to count all matching rows. If your UI only needs "Previous" and "Next" links, use simple_paginate instead. It skips the count query on large tables and returns a SimplePaginator:

1let subscribers = db::table("subscribers")
2 .order_by("id")
3 .simple_paginate::<Subscriber>(15)
4 .await?;
1let subscribers = db::table("subscribers")
2 .order_by("id")
3 .simple_paginate::<Subscriber>(15)
4 .await?;

Paginating models

You may also paginate model queries directly without specifying a row type, because the query is already typed to the model:

1use laravel::model::model;
2use laravel::pagination::Paginator;
3use laravel::prelude::*;
4use laravel::time::Timestamp;
5 
6#[model]
7pub struct Post {
8 pub id: i64,
9 pub title: String,
10 pub published: bool,
11 pub created_at: Timestamp,
12}
13 
14pub async fn index() -> laravel::Result<Json<Paginator<Post>>> {
15 let posts = Post::query()
16 .filter(Post::published.eq(true))
17 .order_by(Post::created_at.desc())
18 .paginate(15)
19 .await?;
20 
21 Ok(Json(posts))
22}
1use laravel::model::model;
2use laravel::pagination::Paginator;
3use laravel::prelude::*;
4use laravel::time::Timestamp;
5 
6#[model]
7pub struct Post {
8 pub id: i64,
9 pub title: String,
10 pub published: bool,
11 pub created_at: Timestamp,
12}
13 
14pub async fn index() -> laravel::Result<Json<Paginator<Post>>> {
15 let posts = Post::query()
16 .filter(Post::published.eq(true))
17 .order_by(Post::created_at.desc())
18 .paginate(15)
19 .await?;
20 
21 Ok(Json(posts))
22}

Paginated model queries eager-load any relationships passed to with, like get. If you want to transform each model before returning the paginator, such as into a smaller response struct, call map:

1#[derive(Serialize)]
2pub struct PostSummary {
3 id: i64,
4 title: String,
5}
6 
7let posts = Post::query()
8 .order_by(Post::id.desc())
9 .paginate(15)
10 .await?
11 .map(|post| PostSummary { id: post.id, title: post.title });
1#[derive(Serialize)]
2pub struct PostSummary {
3 id: i64,
4 title: String,
5}
6 
7let posts = Post::query()
8 .order_by(Post::id.desc())
9 .paginate(15)
10 .await?
11 .map(|post| PostSummary { id: post.id, title: post.title });

Cursor pagination

Offset-based pagination (paginate and simple_paginate) skips rows with a SQL OFFSET clause. On deep pages, the database still has to scan and discard every skipped row, and if new records are inserted while a user is paging through results, rows shift downward and may appear twice across adjacent pages.

cursor_paginate avoids both issues by encoding the ordered column values of the last row into an opaque cursor query parameter so the next request resumes directly after that row. It returns a CursorPaginator:

1let posts = Post::query()
2 .order_by(Post::created_at.desc())
3 .order_by(Post::id.desc())
4 .cursor_paginate(15)
5 .await?;
1let posts = Post::query()
2 .order_by(Post::created_at.desc())
3 .order_by(Post::id.desc())
4 .cursor_paginate(15)
5 .await?;

For example, on page 500, paginate forces the database to scan 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, cursor_paginate queries directly for rows after the previous page's cursor, taking advantage of an index on the ordered columns. It fetches one extra row to determine 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 cursor pagination only provides previous and next links, use paginate when your UI requires numbered page links.

Order by a combination of columns that uniquely identifies every row, such as a timestamp followed by the primary key. Model queries without an order_by default to ordering by their primary key. Ordered columns must be non-nullable, and db::table queries must select them so the paginator can construct the cursor from the final row.

Paginator methods

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

Method Description
items(), into_items() Returns a slice (&[T]) or owned Vec<T> of the items on the current page.
per_page() Returns the number of items per page.
has_more_pages() Returns whether another page follows the current page.
previous_page_url(), next_page_url() Returns the URL for the previous or next page, or None at either end.
map(f) Transforms each item on the current page using the closure f.
with_query_string(), appends(query), with_path(path) Customizes the generated pagination URLs.

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

Method Description
current_page() Returns the current 1-based page number.
url(page) Generates 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.
last_page() Returns the page number of the final page.
on_each_side(count) Sets how many page links appear on each side of the current page in links.

CursorPaginator (returned by cursor_paginate) adds cursor helpers:

Method Description
previous_cursor(), next_cursor() Returns the encoded cursor for the previous or next page, or None at either end.
on_first_page() Returns whether the current page is the first page.

Customizing pagination URLs

By default, pagination URLs inherit the current request path and append only the page parameter, such as https://example.com/users?page=2. Other query parameters are omitted, so navigating to page 2 drops active search or sort filters. Call with_query_string to preserve the entire query string:

1let users = User::query().order_by(User::name).paginate(15).await?.with_query_string();
1let users = User::query().order_by(User::name).paginate(15).await?.with_query_string();

To preserve only specific parameters, pass them to appends. It accepts key-value pairs such as [("view", "grid")] or a serializable struct such as your query filters, omitting None fields. Using #[laravel::query] ensures an invalid bookmark parameter such as ?sort=bogus is ignored instead of failing the page:

1use laravel::inertia::{self, Page, props};
2use laravel::prelude::*;
3use serde::Serialize;
4 
5#[laravel::query]
6#[derive(Serialize)]
7pub struct Filters {
8 #[validate(max_length(100))]
9 search: Option<String>,
10 #[validate(one_of("name", "newest"))]
11 sort: Option<String>,
12}
13 
14pub async fn index(Query(filters): Query<Filters>) -> laravel::Result<Page> {
15 let users = User::query()
16 .filter(filters.search.as_ref().map(|term| User::name.contains(term)))
17 .order_by(User::name)
18 .paginate(15)
19 .await?
20 .appends(&filters);
21 
22 Ok(inertia::render("Users/Index", props! { users }))
23}
1use laravel::inertia::{self, Page, props};
2use laravel::prelude::*;
3use serde::Serialize;
4 
5#[laravel::query]
6#[derive(Serialize)]
7pub struct Filters {
8 #[validate(max_length(100))]
9 search: Option<String>,
10 #[validate(one_of("name", "newest"))]
11 sort: Option<String>,
12}
13 
14pub async fn index(Query(filters): Query<Filters>) -> laravel::Result<Page> {
15 let users = User::query()
16 .filter(filters.search.as_ref().map(|term| User::name.contains(term)))
17 .order_by(User::name)
18 .paginate(15)
19 .await?
20 .appends(&filters);
21 
22 Ok(inertia::render("Users/Index", props! { users }))
23}

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

1let users = User::query().order_by(User::name).paginate(15).await?.with_path("/admin/users");
1let users = User::query().order_by(User::name).paginate(15).await?.with_path("/admin/users");

Multiple paginators per page

When rendering multiple paginated lists on the same page, assign each one a distinct query parameter with page_name, or cursor_name for cursor pagination, and call with_query_string so paginating one list preserves the other's position:

1let users = User::query()
2 .order_by(User::name)
3 .paginate(15)
4 .page_name("users")
5 .await?
6 .with_query_string();
7 
8let posts = Post::query()
9 .order_by(Post::id.desc())
10 .paginate(15)
11 .page_name("posts")
12 .await?
13 .with_query_string();
1let users = User::query()
2 .order_by(User::name)
3 .paginate(15)
4 .page_name("users")
5 .await?
6 .with_query_string();
7 
8let posts = Post::query()
9 .order_by(Post::id.desc())
10 .paginate(15)
11 .page_name("posts")
12 .await?
13 .with_query_string();

Fetching a specific page

By default, paginators read the page number or cursor from the request's query string. When the page comes from somewhere else, such as a JSON request body, pass it explicitly with page (or cursor for cursor pagination). Both accept an Option, falling back to the request query string when given None:

1use laravel::pagination::Paginator;
2use laravel::prelude::*;
3 
4#[laravel::input]
5pub struct Search {
6 page: Option<u64>,
7}
8 
9pub async fn search(Json(search): Json<Search>) -> laravel::Result<Json<Paginator<User>>> {
10 let users = User::query().order_by(User::id).paginate(15).page(search.page).await?;
11 
12 Ok(Json(users))
13}
1use laravel::pagination::Paginator;
2use laravel::prelude::*;
3 
4#[laravel::input]
5pub struct Search {
6 page: Option<u64>,
7}
8 
9pub async fn search(Json(search): Json<Search>) -> laravel::Result<Json<Paginator<User>>> {
10 let users = User::query().order_by(User::id).paginate(15).page(search.page).await?;
11 
12 Ok(Json(users))
13}

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 construct a Paginator from the page's items, total count, page size, and current page. pagination::current_page reads the current page number from the request query string:

1use laravel::db;
2use laravel::pagination::{self, Paginator};
3use laravel::prelude::*;
4 
5pub async fn index() -> laravel::Result<Json<Paginator<Subscriber>>> {
6 let page = pagination::current_page();
7 
8 let offset = (page - 1) * 15;
9 
10 let subscribers = db::query!(
11 "SELECT id, email FROM subscribers ORDER BY id LIMIT 15 OFFSET {offset}"
12 )
13 .select_as::<Subscriber>()
14 .await?;
15 
16 let total = db::query!("SELECT COUNT(*) FROM subscribers")
17 .scalar::<i64>()
18 .await?
19 .unwrap_or(0);
20 
21 Ok(Json(Paginator::new(subscribers, total as u64, 15, page)))
22}
1use laravel::db;
2use laravel::pagination::{self, Paginator};
3use laravel::prelude::*;
4 
5pub async fn index() -> laravel::Result<Json<Paginator<Subscriber>>> {
6 let page = pagination::current_page();
7 
8 let offset = (page - 1) * 15;
9 
10 let subscribers = db::query!(
11 "SELECT id, email FROM subscribers ORDER BY id LIMIT 15 OFFSET {offset}"
12 )
13 .select_as::<Subscriber>()
14 .await?;
15 
16 let total = db::query!("SELECT COUNT(*) FROM subscribers")
17 .scalar::<i64>()
18 .await?
19 .unwrap_or(0);
20 
21 Ok(Json(Paginator::new(subscribers, total as u64, 15, page)))
22}

Displaying pagination results

Converting results to JSON

Paginators implement Serialize, encoding items under data alongside pagination metadata and URLs. Return a paginator directly with Json; page 2 of paginate(15) serializes 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": 30,
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": 30,
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 on_each_side on the paginator to adjust how many pages appear on each side of the current page:

1let users = User::query()
2 .order_by(User::name)
3 .paginate(15)
4 .await?
5 .on_each_side(1);
1let users = User::query()
2 .order_by(User::name)
3 .paginate(15)
4 .await?
5 .on_each_side(1);

For example, on page 10 of 20 with on_each_side(1), the links array produces 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. CursorPaginator serializes data, path, per_page, next_cursor, next_page_url, prev_cursor, and prev_page_url.

In a view, pass the paginator in your template data, such as data! { users }, then loop over its links array. Mark each label with the safe filter so the HTML entities in "Previous" and "Next" render properly:

1<nav>
2 {% for link in users.links %}
3 {% if link.url %}
4 <a
5 href="{{ link.url }}"
6 {% if link.active %}aria-current="page"{% endif %}
7 >{{ link.label | safe }}</a>
8 {% else %}
9 <span>{{ link.label | safe }}</span>
10 {% endif %}
11 {% endfor %}
12</nav>
1<nav>
2 {% for link in users.links %}
3 {% if link.url %}
4 <a
5 href="{{ link.url }}"
6 {% if link.active %}aria-current="page"{% endif %}
7 >{{ link.label | safe }}</a>
8 {% else %}
9 <span>{{ link.label | safe }}</span>
10 {% endif %}
11 {% endfor %}
12</nav>

When multiple views render pagination links, you can extract this markup into a template macro:

1<!-- resources/views/components/pagination.html -->
2 
3{% macro pagination(paginator) %}
4 <nav>
5 {% for link in paginator.links %}
6 {% if link.url %}
7 <a
8 href="{{ link.url }}"
9 {% if link.active %}aria-current="page"{% endif %}
10 >{{ link.label | safe }}</a>
11 {% else %}
12 <span>{{ link.label | safe }}</span>
13 {% endif %}
14 {% endfor %}
15 </nav>
16{% endmacro %}
1<!-- resources/views/components/pagination.html -->
2 
3{% macro pagination(paginator) %}
4 <nav>
5 {% for link in paginator.links %}
6 {% if link.url %}
7 <a
8 href="{{ link.url }}"
9 {% if link.active %}aria-current="page"{% endif %}
10 >{{ link.label | safe }}</a>
11 {% else %}
12 <span>{{ link.label | safe }}</span>
13 {% endif %}
14 {% endfor %}
15 </nav>
16{% endmacro %}

Then import and call the macro from any view:

1{% from "components.pagination" import pagination %}
2 
3{{ pagination(users) }}
1{% from "components.pagination" import pagination %}
2 
3{{ pagination(users) }}

Pass the paginator as a prop, such as props! { 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 the paginator through inertia::scroll_with, which takes a callback that fetches the page:

1use laravel::inertia::{self, Page, props};
2 
3pub async fn index() -> Page {
4 inertia::render("Posts/Index", props! {
5 posts => inertia::scroll_with(async || {
6 Post::query().order_by(Post::id.desc()).cursor_paginate(15).await
7 }),
8 authors => inertia::lazy(async || User::query().order_by(User::name).get().await),
9 })
10}
1use laravel::inertia::{self, Page, props};
2 
3pub async fn index() -> Page {
4 inertia::render("Posts/Index", props! {
5 posts => inertia::scroll_with(async || {
6 Post::query().order_by(Post::id.desc()).cursor_paginate(15).await
7 }),
8 authors => inertia::lazy(async || User::query().order_by(User::name).get().await),
9 })
10}

To load the next page, the component requests only the posts prop. Because Inertia evaluates prop closures only for requested props, scroll requests execute the posts query while skipping the authors query.

inertia::scroll(posts) wraps an already-fetched paginator. Because eagerly evaluated props run before Inertia inspects which props were requested, prefer scroll_with 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.