How to update nested models in runtime?


#1

Hi everyone, how are you?

I am new with adonis and i’m trying to build a simple app, but with some features.

My question is: How can i update a nested lucid model which belongs to a collection in runtime?

Ex of my code:


const User = use('App/Models/User')

let user = await User.with('carts').first()


let cart = user.carts[0]
cart.total = 250
await cart.save()

But the code above does not work, anyone can help me with the best way to do this?


#2

Try getting relation, then update:


let cart = user.getRelated('carts').rows[0]
cart.total = 250
await cart.save()

#3

Thanks, worked!