Crash Courses · Basic
Collections & Fields
Define content types with Payload collections and field types.
11 min read+150 XP on completionCert: Payload CMS Crash Course
Tap any word in the text below to start reading from there.
Collections & Fields
Collections define your content types. Fields define the shape of each document within the collection.
Basic Collection
// collections/Posts.ts
import type { CollectionConfig } from 'payload'
export const Posts: CollectionConfig = {
slug: 'posts',
admin: {
useAsTitle: 'title',
defaultColumns: ['title', 'status', 'author', 'publishedAt'],
},
fields: [
{
name: 'title',
type: 'text',
required: true,
},
{
name: 'slug',
type: 'text',
required: true,
unique: true,
index: true,
admin: { position: 'sidebar' },
},
{
name: 'status',
type: 'select',
options: ['draft', 'published', 'archived'],
defaultValue: 'draft',
admin: { position: 'sidebar' },
},
{
name: 'author',
type: 'relationship',
relationTo: 'users',
required: true,
},
{
name: 'content',
type: 'richText',
},
{
name: 'tags',
type: 'array',
fields: [
{ name: 'tag', type: 'text', required: true },
],
},
{
name: 'publishedAt',
type: 'date',
admin: { position: 'sidebar' },
},
],
}Field Types Reference
// Common field types
{ name: 'title', type: 'text' }
{ name: 'body', type: 'richText' }
{ name: 'xp', type: 'number', min: 0, max: 1000 }
{ name: 'active', type: 'checkbox', defaultValue: true }
{ name: 'level', type: 'select', options: ['Basic', 'Masters', 'PhD'] }
{ name: 'author', type: 'relationship', relationTo: 'users' }
{ name: 'cover', type: 'upload', relationTo: 'media' }
{ name: 'publishedAt', type: 'date' }
{ name: 'metadata', type: 'json' }
{ name: 'email', type: 'email' }
{ name: 'url', type: 'text', validate: (val) => isURL(val) || 'Must be a valid URL' }Reading progress
0% read
In this module
Collection
Field
slug
Relationship field
0%