Create a Collection
Collection in PayloadCMS is a group of data that has the same structure. For example, if you have a blog, you can create a collection called posts that contains all the blog posts. Each post will have the same structure, such as title, content, author, etc.
How to create a collection
-
Create a new file named
Posts.tsinside thecollectionsfolder.Directorypublic/
- …
- src
Directoryapp
Directory(landing)/
- …
Directorycollections
- Posts.ts
- package.json
- next.config.ts
Here an example of the Post Collection :
src/collections/Posts.js import type { CollectionConfig } from "payload";export const Posts: CollectionConfig = {slug: "post",access: {read: () => true,},admin: {useAsTitle: "title",},fields: [{name: "title",type: "text",required: true,},{name: "body",type: "richText",required: true,},{name: "image",type: "upload",relationTo: "media",required: true,},],}; -
Add the Posts collection to the
collectionsarray in thepayload.config.tsfile.src/payload.config.ts import { Posts } from './collections/Posts'export default buildConfig({// ...otherConfig,collections: [User, Media, Posts],}) -
You’re set! Now check out the dashboard and you’ll see the new Post Collections.