I have a collection called 'Votes' where a user will vote on another location called 'Locations'. Each Vote will have a locationId and UserId. Would it be possible to update a total tally of votes in the related location collection after a vote document is created using a Hook? I'm not qutie sure if they're only intended for updating document in the same collection or not.
I'm thinking Sibling Data is what I need to use here:
import type { FieldHook } from 'payload/types';
// Field hook type is a generic that takes three arguments:
// 1: The document type
// 2: The value type
// 3: The sibling data type
type ExampleFieldHook = FieldHook<ExampleDocumentType, string, SiblingDataType>;
const exampleFieldHook: ExampleFieldHook = (args) => {
const {
value, // Typed as
string
as shown above
data, // Typed as a Partial of your ExampleDocumentType
siblingData, // Typed as a Partial of SiblingDataType
originalDoc, // Typed as ExampleDocumentType
operation,
req,
} = args;
// Do something here...
return value; // should return a string as typed above, undefined, or null
}
You could do this with a field hook or a collection hook. They can be used to update
othercollections, not just the current 👍
Hello. This is a pretty old thread I have a similar desire as the vote things described by Will. My case is when an Asset collection item is updated I want to create a AssetHistory record, stamping the Asset's data at a point in time throughout it's life. The afterChange hook is what I need on the Asset Collection and I understand how to create the afterChange trigger. BUT my question in regard to this subject is do you just straight up make axios/fetch calls to the graphql or REST api to add new records to other collections or is there a fancy payload package or utility to do this?
Apllogies I'm just new to payload. I recently had a demo by your team at our office and I'm building an internal tool with Payload atm to check it out!. I think though I would likely use the
Local API
now reading the docs more
https://payloadcms.com/docs/local-api/overviewmy main goal is just follow best practice for payload dev. I feel like this is the right way to go about updating a different collection from a afterChange hook?
You are correct.
Star
Discord
online
Get dedicated engineering support directly from the Payload team..