Introduction
Payload CMS uses collections to group and structure data, similar to database tables. In this guide, we’ll explore how to define collections, customize fields, and set up basic relationships.
What is a Collection?
A collection in Payload CMS is a way to organize similar data types, such as Posts, Authors, and Categories. Each collection can have its own structure defined by fields, which represent individual data points like titles, dates, or content.
Setting Up a Collection
To create a collection, navigate to your Payload CMS project’s collections folder and add a configuration file, e.g., posts.ts, for the Posts collection.
Defining Fields in Collections
Fields define the structure of data stored within a collection. Payload CMS supports various field types:
- Text: For simple text data, like title.
- RichText: For formatted text content.
- Date: For storing date values.
import { CollectionConfig } from 'payload/types';
const Posts: CollectionConfig = {
slug: 'posts',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
required: true,
},
{
name: 'content',
type: 'richText',
},
{
name: 'publishedDate',
type: 'date',
},
],
};
export default Posts;
Conclusion
Defining collections in Payload CMS allows you to structure and manage your data with flexibility and organization. By setting up well-structured collections with diverse fields, you can build a solid foundation for any application that relies on organized, scalable content. Adding relationships between collections enhances data connectivity, allowing you to create complex, linked data models with ease. Mastering collections in Payload CMS opens up a range of possibilities for content-rich projects and helps you maintain a clean, easily navigable database structure.