Lucid - Search Methods

Hi there,

Every time, when I used, a static search method (find, findOrCreate, where, etc), I supposed that, it isn’t return a model instance, because, when I had tried use fill, save or any other model method, adonis return “is not a function”.

For example:

let product_shop = ProductShop.find({ 'shop_id': shop.id },  { 'product_id': product.id })

Where product_shop it is pivot model, between Product and Shop, following, we have:

product_shop.fill({ price: requestInfo.price }, { quantity: requestInfo.quantity })

OR:

product_shop.fill({ price: requestInfo.price }, { quantity: requestInfo.quantity })

product_shop.price = requestInfo.price
product_shop.quantity = requestInfo.quantity
await product_shop.save()

 
In both case, Adonis return "it is not a function". I really want use only model's methods. I've seen other solutions using Database or updating in row, but, I don't like doing that,

Anyone can help-me? @virk
const product_shop = await ProductShop.query()
                           .where('shop_id', shop_id)
                           .where('product_id', product_id)
                           .firstOrFail()


// Assuming requestInfo is _actually_ a thing...
product_shop.price = requestInfo.price
product_shop.quantity = requestInfo.quantity

// OR lets say this is an update handler, 
// and price/quantity are in the request:
product_shop.fill(request.only(['price', 'quantity']))
await product_shop.save()
1 Like

Hi,

It’s works,

Tks