hasManyrelationship is a
one-to-manyrelationship. For instance, a
Usermay have multiple
hasManyis a WireBox mapping to the related entity.
Postentity is assumed to have a
userIdforeign key. You can override this by passing a foreign key in as the second argument:
idas its primary key, or you wish to join the child entity to a different column, you may pass a third argument to the
hasManymethod specifying your parent table's custom key.
savemethod on the relationship passing in an entity to relate.
Userentity's id as a foreign key in the
Postand save the
Postto the database.
savemethod is called on the
postsrelationship, not the
hasManyrelationship by calling
saveMany. This method takes an array of key values or entities and will associate each of them with the base entity.
createmethod to create and save a related entity directly through the relationship.
"set" & relationshipNameand passing in an array of entities or key values.
4. Any other posts would now be disassociated with this user. Likely your database will be guarding against creating these orphan records. Admittedly, this method is not as likely to be used as the others, but it does exist if it solves your use case.
entityName() & keyNames()