After using Repetear, 2x drops, I wonder why
I have a stock status, let's say stock 60, I want to drop 2 pieces, I enter the data 2*2=4 pieces from the stock. I didn't understand why it fell, my codes are like this.
18 Replies
ensure your values are integers when calculating and also provider the failing message
The point I do not understand is that this total also knows that it should fall by 2, the value is correct, but as soon as it is updated, it falls by 4.
Check your network logs is the query running twice? Once with each adjustment?
it looks like this
this code was returning 2 times, it was revealed when I logged it, but I don't understand why it returns 2 times.
It depends how the logic is determined, you shouldn't really be updating the database from a validation rule? You should be doing it on save or on afterStateUpdated
I wonder how I can fix this, I'm new to filament. This code block needs to update the stock quantity. According to the data I get from the user, but I'm not sure where else I can do this.
Can you explain where you are doing this? Is it in a form?
For example, you are doing it on a validation rule. So say they change it again it will reduce down again. You should do it on an updating action
I moved this part to afterstateupdate, but this part is not triggered here either, but when there is an update, I feel that I am doing quite wrong today π¦
Add ->reactive() or ->lazy() to your TextInput.
It happened when I added reactive, but I could not make the code I shared above compatible with this. I wonder if it happens because the $get function does not come, what kind of correction should I make?
->afterStateUpdated(function ($get) {
return function ($attribute, $value, $fail) use ($get) {
Log::info('deneme 2');
$productOrder = ProductOrder::find($get('product_order_id'));
if ($productOrder->amount >= $value) {
$de=$productOrder->amount;
$total=$de-intval($value);
$productOrder->update(['amount' => $total]);
}
};
})
what's calling the function that you are returning?
I solved it this way thank you very much for your help
yea, that looks better. π
Much better!
Hello, I will bother again. I want this code to work when you press the submit button. It works as soon as a value is entered in the field. I wonder if there is a solution?
Maintaining a running stock quantity counter is not entirely trivial to do by using form submission data, as you have to take a lot of potential failure modes into account. After doing this a lot of times over the years, my approach is not to try and do it by subtraction and addition of individual purchase / restock amounts, but rather just re-calculate it from first principles whenever anything happens that might change it.
By which I mean, rather than running a query which subtracts the order quantity on the submitted form from a running total on the product (or stock) table when the purchase form is submitted, rather run a query which sums the total of ALL purchase, sums the total of ALL restocks, and derives the stock quantity from that.
So, for example, if you have a 'purchases' table and a 'restocks' table which track product leaving and entering inventory, and a 'products' table which tracks stock quantity, the (pseudo code) query would look like ...
update products set stock_qty = (select sum(qty) from restocks where product_id = 123) - (select sum(qty) from purchases where product_id = 123) where id = 123
And for stuff like this, I don't use Filament itself to run that query, I use Laravel's built in model create/update events. That is, a boot() method on the 'purchases' and 'restocks' tables, which catch the 'updated' and 'created' events, and runs a common helper method to run the stock_qty update query.