Given for example, a list of products with an attribute of sold_out, I would like to update a field of every item in that collection.
In this particular example, let's suppose I want to set the field sold_out = false
to all items with that field set as true
:
Product.where({sold_out: true})
.fetchAll()
.then(soldOutCollection => {
return Promise.all(product => {
return product.save({sold_out: false})
})
})
This works, but it triggers one query per item in the collection.
Is there any way to update all items at once (triggering just one query)?
PS: I'm trying to avoid using knex.js directly
I think if you want to update this way, you might want to try this:
Product
.where({sold_out: true})
.save(
{sold_out: false},
{method: 'update', patch: true}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With