Skip to content

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

  1. Create a new file named Posts.ts inside the collections folder.

    • 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,
    },
    ],
    };
  2. Add the Posts collection to the collections array in the payload.config.ts file.

    src/payload.config.ts
    import { Posts } from './collections/Posts'
    export default buildConfig({
    // ...otherConfig,
    collections: [User, Media, Posts],
    })
  3. You’re set! Now check out the dashboard and you’ll see the new Post Collections.