Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Relationship field, show title of relationship instead of id

default discord avatar
drojkindlast year
9

When adding a relationship I would like to show the title in the item selector instead of the id, how can I achieve this.



Collection field looks like this:



        {
            name: 'participated',
            label: 'Participated',
            type: 'relationship',
            hasMany: true,
            relationTo: [
                'camps', 'labs', 'sessions'
            ],
            admin: {
                allowCreate: false,
            }
        }
  • default discord avatar
    yhn5790last year
    @208349282577809408

    You can make this change on the relevant collection. Use

    useAsTitle



    Quoting the doc



    Specify a top-level field to use for a document title throughout the Admin panel. If no field is defined, the ID of the document is used as the title.


    https://payloadcms.com/docs/configuration/collections#admin-options
  • default discord avatar
    drojkindlast year
    @541306947463282698

    saved the day, been banging my head for weeks. Assumed it was on the relationship type itself. THANK YOU!!!!

    :partyblob:
  • default discord avatar
    hendrik01last year
    @541306947463282698

    do u know if there is the possibilty to join values for 'useAsTitle'? For example displaying a relationship as part of the title. [Manufacturer.name] + [name]

  • default discord avatar
    etmartinkazoolast year
    @441722987817730058

    I was looking for this today as well. Still haven't found an answer yet.

  • default discord avatar
    yhn5790last year
    @441722987817730058

    @1005175744474927194

    Have you tried virtual fields?

    https://payloadcms.com/blog/learn-how-virtual-fields-can-help-solve-common-cms-challenges
  • default discord avatar
    hendrik01last year
    @541306947463282698

    ah this helps, thanks!

  • default discord avatar
    jm.svlast year
    @441722987817730058

    when you were digging in to this, did you find any way to render something other than text in the relationship dropdown? I'm trying to show an image thumbnail but haven't found a way to customize the dropdown item components

  • default discord avatar
    hendrik01last year

    Not yet. I only needed to display other text

  • default discord avatar
    0xptlast year

    This helped me a lot! It would be great if we could add something to the docs for Relationships, since the default of showing an ID is an unpleasant look, and I had to look in Discord to find this

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.