Picture of the author

Notion Databases

Query Databases

Database queries

Every database query returns a PageCollection::class, which can be used either as an iterable Collection of the pages pages or as a JSON-String, suited for frontend handling.

$pageCollection = Notion::database($databaseId)->query();

$collectionOfPages = $pageCollection->asCollection();
$jsonOfPages = $pagesCollection->asJson();

In order to understand the full context of handling multiple results, please refer to Handling Results .

How to handle Pages and their Properties is fully explained in Fetch Pages and Handle Properties .

Filtering a database query

You can pretty much filter database queries like in Notion.

Notion::database($databaseId)
        ->filterBy($filters) // filters are optional
        ->sortBy($sortings) // sorts are optional
        ->limit(5) // limit is optional
        ->query()
        ->asCollection();

Please refer to Filtering and Sorting , on how to handle filtering and sorting in detail.

Pagination

Query responses of the Notion API are always limited to a maximum of 100 results. Results can also be limited to a lesser amount which makes pagination necessary.

Getting results further into the dataset can be achieved by using an offset.

$response = Notion::database($databaseId)->query();

$startCursor = $response->getNextCursor();

$responseWithOffset = Notion::database($databaseId)
        ->offset($startCursor)
        ->query();

Please refer to Pagination for more details.

Previous
Create Database