Answer the question
In order to leave comments, you need to log in
What is the best way to update data in Laravel models?
The task is to make prices for goods with the possibility of converting depending on the exchange rate.
Data on the cost of goods and exchange rates are stored in different tables with a one-to-many relationship.
To store data on the cost of goods, the following fields have been created in the table:
Answer the question
In order to leave comments, you need to log in
Thank you all for your help and information in solving the problem.
Given that this information may be useful to others, I will describe the solution I chose.
Indeed, the task of regularly updating prices depending on the exchange rate can be solved in different ways. For myself, I chose the way to update the data using raw database queries. Given that the exchange rate data is not updated often, only once a day, I think this will be the best, not resource-intensive way.
Product data, including their cost, is stored in the 'product' table, which also contains fields for determining the price of the product:
$currency = Currency::select('currency', 'Nominal', 'value')->get()->keyBy('currency')->toArray();
$countUpdate = DB::update("UPDATE `product` SET `price` = (`base_price` * ?) WHERE `currency`='EUR'", [ $currency['EUR']['value'] ]);
1. Make a function that will receive the price value and currency type as input... and then multiply by the rate of this currency when outputting ...
2. The rate itself can be updated once in a while (at least once every 10 minutes) - not an expensive operation;
3. In the place where the price will be used, use the function from point 1 .
The operation of multiplication during the output will not spend a lot of resources ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question