Relationship Operations

for

  • Arguments: (...args)
  • Returns: self

Create a new related model.

const post = await Post.find(1)
const comment = new Comment({
  text: 'New comment for this post!'
}).for(post)

await comment.save()
POST /posts/1/comments
const user = new User({ id: 1 })
const post = await user.posts().first()
const comment = new Comment({
  text: 'New comment for this post!'
}).for(user, post)

await comment.save()
POST /users/1/posts/1/comments

attach

  • Arguments: (payload)
  • Returns: Model | { data: Model }

Another way to create a new related model.

const post = await Post.find(1)
const comment = post.comments().attach({
  text: 'New comment for this post!'
})
POST /posts/1/comments

sync

  • Arguments: (payload)
  • Returns: Model | { data: Model }

Update a related model.

const post = await Post.find(1)
const comment = await post.comments().sync({
  text: 'Updated comment!'
})
PUT /posts/1/comments