Relationship field, show title of relationship instead of id

default discord avatar
drojkind
3 months ago
8

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
    yhn5790
    3 months ago

    @drojkind 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
    drojkind
    3 months ago

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

    :partyblob:
  • default discord avatar
    hendrik01
    3 months ago

    @yhn5790 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
    etmartinkazoo
    3 months ago

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

  • default discord avatar
    yhn5790
    3 months ago
  • default discord avatar
    hendrik01
    3 months ago

    @yhn5790 ah this helps, thanks!

  • default discord avatar
    jm.sv
    3 months ago

    @hendrik01 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
    hendrik01
    2 months ago

    Not yet. I only needed to display other text

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.