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.

how to count the number

default discord avatar
clhomelast year
1 1

Hello everyone, I am a newbie
in collections,I create a c.ts,it contain C1,C2,C3,Now I wanna make c3=c1+c2,how to get c1,c2 value in c3's hook, pls help to how to do it,thanks,
my code is below:
`
import { CollectionConfig, Field } from 'payload/types';

const Tags: CollectionConfig = {
slug: 'c_sum',

admin: {
useAsTitle: 'name',
defaultColumns: ['name', 'c1','c2','c_sum']
},
access: {
read: () => true,
create:()=>true,
},
fields: [
{
type: 'text',
name: 'name',
},
{
type: 'number',
name: 'c1',
label: "c1",
},
{
type: 'number',
name: 'c2',
label: "c2",
},
{
type: 'number',
name: 'c_sum',
label: "c_sum",
},

],
timestamps: false,
}
export default Tags;`

  • Selected Answer
    default discord avatar
    clhomelast year

    i solved:

    {
          type: 'number',
          name: 'c_sum',
          label: "c_sum",
          admin: {
            description: 'c1+c2',
            hidden:false
            
          },
          access: {
            update: () => false,
            create:()=>false
            
          },
          hooks: {
            afterRead: [
              ({ data }) => {  
                return data.c1 + data.c2;            
              }
            ],
          },
        },
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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