Update or merge?


Yo. Just a quick question. Whats the difference between using the .update({ }) and .merge() function? Is one prefered over the other?


merge() will only update params that you specify for a single record and it will run lifecycle methods of model.

By using update, you can bulk update multiple records in database, BUT it will never execute any model hooks which is really important to know.


Hey! :wave:

Actually .merge() will not perform a database updates. It will merge the parameter you gave him with the one already defined. Then you can use save() to persist it into the database.


Infact update and merge methods exists on different Classes altogether.

The merge method is available on a model instance, example

const user = new User()
user.merge({ name: 'foo'  })

await user.save()

Whereas, update is available on the query builder instance

await User
  .where('age', '>', 18)
  .update({ allowed: true })