How to Increament a row value before update


#1

Hellos I am just wondering if their is away I can use to increment a row value using current row value before updating i.e

 this.addHook('beforeSave', async (modelInstance) => {
        let increament_val = (current_row_value)
        if (modelInstance.dirty.paid) {
            modelInstance.paid = modelInstance.paid + increament_val
        }
      })
    }'

@virk


#2

What’s the issue with the code you are running. Also you want to increment to every update?


#3

Thanks for replying, no issues with the code, I just want to know how I can increment an entry value before update. Yes all updates

@virk


#4

Hi @fredrickreuben

You can probably learn more about Lifecycle Events here https://adonisjs.com/docs/4.0/database-hooks#_lifecycle_events

You can actually use beforeUpdate and afterUpdate in your models.