Updating an embedsMany child directly #2618
Unanswered
chrispage1
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Do you need to retrieve the model data or can you update the database directly? This can be done with 2 queries // Update the document where sku matches in the root document
YourModel::where('sku', $skuToUpdate)
->update(['stock' => $newStockValue]);
// Update the document within the "variants" array
YourModel::where('variants.sku', $skuToUpdate)
->update(['variants.$.stock' => $newStockValue]); But I think it's possible to combine them using the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I've got a database of products and each product has multiple variants. A variant is also a Product model. There are about 2000 products, and 3000 variants.
Periodically, I'm having to loop through the data and update stock values. We get a stock spreadsheet with SKU and corresponding stock values.
To get the relevant model, I'm performing the below:
The problem is, ideally I'd like my query to return the child itself, but in all cases it'd still return the parent and then I'd have to drill down into the variants making the process very inefficient. Is there any way I am able to make this more efficient by querying the variants directly?
Chris.
Beta Was this translation helpful? Give feedback.
All reactions