Notion Databases
Pagination
In order to handle databases with more than 100 results, pagination is crucial.
Use pagination within database queries
The following example describes the simplest form of pagination: Accessing the first 100 entries, followed by accessing the next 100 entries.
# query the database (1 - 100 entries)
$response = Notion::database($databaseId)->query();
# get StartCursor::class object, which can be used to get next page of pagination
$startCursor = $response->getNextCursor();
# get next iteration of pagination, with $startCursor (entries 101-200)
$responseWithOffset = Notion::database($databaseId)
->offset($startCursor)
->query();
Helpers
Use last response as offset
As an alternative to calling getNextCursor()
, the previous response can be used to query the next iteration of the pagination.
# query the database (entries 1-100)
$previousResponse = Notion::database($databaseId)->query();
# query the database further (entries 101-200)
$responseWithOffset = Notion::database($databaseId)
->offsetByResponse($previousResponse)
->query();
Raw next cursor
In case the raw next cursor (contains the UUID of the next page as a string) needs to be accessed, you can use the getRawNextCursor()
method.
$nextPageUuid = $response->getRawNextCursor();
Limit responses
When a limitation of page results is required, the method limit($limit)
can be used.
# query the database (1 - 5 entries)
$response = Notion::database($databaseId)
->query()
->limit(5);