Displaying RelationTo Data by Name

default discord avatar
taun2160
2 months ago
2

How do I display the Category Names in the dropdown, as opposed to their ID string? Currently they're labeled as Untitled, though they have label names.



import { CollectionConfig } from 'payload/types';

const Items: CollectionConfig = {
  slug: 'items',
  admin: {
    useAsTitle: 'name',
  },
  access: {
    read: () => true,
  },
  fields: [
    {
      name: 'name',
      type: 'text',
    },
    {
      name: 'description',
      type: 'text',
    },
    {
      name: 'category',
      type: "relationship",
      relationTo: "portfolioCategories",
    },
  ],
  timestamps: false,
}

export default Items;




import { CollectionConfig } from 'payload/types';

const PortfolioCategories: CollectionConfig = {
  slug: 'portfolioCategories',
  admin: {
    useAsTitle: 'title',
  },
  access: {
    read: () => true,
  },
  fields: [
    {
      name: 'name',
      label: 'Portfolio Categories',
      type: 'text',
    },
  ],
  timestamps: false,
}

export default PortfolioCategories;
  • default discord avatar
    kvist__
    2 months ago

    useAsTitle needs to be a field on the collection - use "name" instead of "title" 🙂

  • default discord avatar
    taun2160
    2 months ago

    That worked, thanks.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.