Database: Pagination
Introduction
Mongoloquent's paginator is integrated with the query builder and Mongoloquent ORM and provides convenient, easy-to-use pagination of database records with zero configuration.
Basic Usage
Paginating Query Builder Results
There are several ways to paginate items. The simplest is by using the paginate
method on the query builder.
- Typescript
- Javascript
import { DB, IMongoloquentSchema, IMongoloquentTimestamps } from 'mongoloquent';
interface IUser extends IMongoloquentSchema, IMongoloquentTimestamps {
name: string;
}
const users = await DB.collection<IUser>('users')
.paginate(1, 15);
import { DB } from 'mongoloquent';
const users = await DB.collection('users')
.paginate(1, 15);
Paginating Mongoloquent Results
You may also paginate Mongoloquent queries. In this example, we will paginate the Models/User
model and indicate that we plan to display 15 records per page. As you can see, the syntax is nearly identical to paginating query builder results:
- Typescript
- Javascript
import User from './Models/User';
const users = await User
.paginate(1, 15);
import User from './Models/User';
const users = await User
.paginate(1, 15);
Of course, you may call the paginate
method after setting other constraints on the query, such as where
clauses:
- Typescript
- Javascript
import User from './Models/User';
const users = await User
.where("votes", ">", 100)
.paginate(1, 15);
import User from './Models/User';
const users = await User
.where('votes', '>', 100)
.paginate(1, 15);
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
_