Document Nesting With Payload's Nested Docs Plugin

The Payload CMS Nested Docs plugin allows you to organize and manage nested documents within your content structure. By using this plugin, you can create hierarchical relationships between different collections, making it easier to handle nested data models and maintain a clean content structure.
With the Nested Docs plugin you can easily link and manage content from different collections in a parent-child relationship, ensuring that your data is well-organized and accessible throughout the CMS.
In this article I'll covering the following:
- A Documentation Collection model using Nested Docs plugin
- Nesting categories using the Nested Docs plugin
- How to build a breadcrumbs path using the Nested Docs plugin
- How to query nested documents
- How to use Next.js with the Nested Docs plugin
Getting Started
If you would like to follow along, you can generate a Payload project with the following command.
1npx create-payload-app
Installing the Payload Nested plugin
In order to add nested Collection support, the plugin needs to be installed.
1pnpm add @payloadcms/plugin-nested-docs
The plugin needs to be added to the main payload.config.ts file. Later in the article, we'll add and configure the Collections.
1import { nestedDocsPlugin } from '@payloadcms/plugin-nested-docs'2import { buildConfig } from 'payload'34export default buildConfig({5 plugins: [6 nestedDocsPlugin({7 // nested config8 }),9 ],10})
Building a Document Collection model
For this example I'll create a basic Documentation system which will have the following features:
- Document status: A select menu to set a single relationship to a document progress status indicator.
- Document tags: Create a Tags Collection to hold many tags that might be attached by documentation authors and editors.
- Nested categories: Uses the Payload
@payloadcms/plugin-nested-docsplugin to automatically add nested docs support with breadcrumbs.
The Document Collection
Here's the documents Collection model for this example.
1import type { CollectionConfig } from 'payload'2import { lexicalEditor } from '@payloadcms/richtext-lexical'34export const Documents: CollectionConfig = {5 slug: 'documents',6 admin: {7 useAsTitle: 'title',8 },9 fields: [10 {11 name: 'slug',12 type: 'text',13 admin: {14 position: 'sidebar'15 },16 hooks: {17 beforeChange: [18 ({value, operation, data}) => {19 if (operation === 'create' || operation === 'update') {20 if (data && !data?.slug) {21 return data.title.trim().toLowerCase().replace(/[^a-zA-Z0-9]/g, ' ').replaceAll(' ', '-')22 }23 }24 return value25 },26 ],27 },28 },29 {30 name: 'title',31 type: 'text',32 required: true,33 },34 {35 name: 'content',36 type: 'richText',37 required: true,38 editor: lexicalEditor(),39 },40 {41 name: 'documentStatus',42 type: 'select',43 interfaceName: 'DocumentStatus',44 admin: {45 position: 'sidebar',46 },47 hasMany: false,48 options: [49 {50 label: 'On hold',51 value: 'on-hold',52 },53 {54 label: 'Rough draft',55 value: 'rough-draft',56 },57 {58 label: 'In progress',59 value: 'in-progress',60 },61 {62 label: 'Read for review',63 value: 'ready-for-review',64 },65 {66 label: 'Approved',67 value: 'approved',68 },69 ],70 },71 {72 name: 'tags',73 type: 'relationship',74 hasMany: true,75 relationTo: 'tags',76 },77 {78 name: 'categories',79 type: 'relationship',80 admin: {81 position: 'sidebar',82 },83 hasMany: false,84 relationTo: 'categories',85 },86 ]87}









