If I understand correctly. You need to update the stock_qty every time there's an insert/update on the item_restock table, grabbing the sum of item_restock.restock_qty and saving it to items.stock_qty. Am I right?
If that's the case, you could combine model observers with database transactions. Observing the createdcreated and updatedupdated events on the ItemRestock model and using transactions to update the Items model
Transactions are very simple:
use Illuminate\Support\Facades\DB;
DB::transaction(function () {
// do your updates here
});
use Illuminate\Support\Facades\DB;
DB::transaction(function () {
// do your updates here
});
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Not sure about checking for them but you can use atomic locks to fight them. Here's a video explaining how the work (Povilas to the rescue) https://www.youtube.com/watch?v=zybiFatkoCo