Iterating over related data in controller


#1

Hi all, in my controller I am grabbing a model with related (through pivot), if i return the data everything is fine however I need to manipulate the data into something else before returning it so I need to loop through the related items and push things to an array to return but I get the error is not iterable.
I’m sure i’m missing something simple but I am also new to js so any help would be appreciated.

let make = await Make.query().with('years', (builder) => {
      builder.select(['id', 'year'])
    }).where('make_id', id).first()
for (const year of make.years) {
//do stuff
}

#2

solved it myself in the end, I needed to convert to json first, i had tried that but was putting it at the end instead of after the main returned data.

for (const year of make.toJSON().years) {