G
G
Glory2022-04-20 15:57:15
Laravel
Glory, 2022-04-20 15:57:15

How to add a bunch of records but if the values ​​have changed?

did not find a pleasant way to add records to the database in batches if you need to add only those values ​​for which one field (price) has changed.

That is, the table stores the prices of goods by date. and every time when the price of a product changes, you need to add a new record. one by one, it is easy to update this table. But if we need to immediately add a bunch of records comparing each record with the last element for each product, how to do it?

At the input we have an array with the fields product_id, price and in the table the same fields + more dates.

add a record only if the price has changed. and you need to do it without a loop.

Or can it first read a slice of the latest prices from the table, compare it with an array and add those that you need?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2022-04-20
@Fragster

you read in batches, then you filter your array, for example, by collection methods (leave only the changed ones), supplement with the date, other necessary columns (if necessary), do Model::create($YourPreparedArray)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question