Skip to main content
Version: v2.x.x

Pagination

Basic Usage

Paginating Query Builder Results

There are several ways to paginate items. The simplest is by using the paginate method. The paginate method automatically takes care of setting the query's "limit" and "offset" based on the current page being viewed by the user.

The paginate method requires two arguments. The first argument is page number and the second argument is the number of items you would like displayed "per page". In this case, let's specify that we would like to display 15 items per page:

import User from "./yourPath/User";

const user = await User.paginate(1, 15);

Of course, you may call the paginate method after setting other constraints on the query, such as where clauses:

const user = await User.where("votes", ">", 100).paginate(1, 15);

Paginate Results

THe paginate method will include meta information such as total, page, lastPage, and perPage. The result records are available via the data key in the JSON array. Here is an example of the data returned by returning a paginate method:

 {
"data": [
{
// Record...
},
{
// Record...
}
],
"meta": {
"total": 50,
"page": 1,
"perPage": 15,
"lastPage": 4,
},
};


Support us

Mongoloquent is an MIT-licensed open source project. It can grow thanks to the support by these awesome people. If you'd like to join them, please read more here.

Sponsors

_

Partners