How to calculate field value based on two other fields?
Hi, I have 3 fields: vehicle price, down payment and loan amount. When I fill vehicle price and down payment, I want to set loan amount value as vehicle price minus down payment.
11 Replies
Use a closure, like
as both function are same in afterStateUpdate, how to remove this duplicacy, also what if my field is select, how to get field select name based on its id?
What do you mean Roni? They are the same but they are different in that the above needs a reaction to loan_amount on either change. If you only did it on price it wouldn 't change on down_payment
Yes I understood. Thanks. And how to get “name” of select field based on its option id. For example if options array is [1=> ‘yes’ , 2 => ‘no’ ] , how to get name “yes” if user selects 1 in after state update function.
Ahh I don’t think you can, you need to have the options stored within the component/resource or a database query and relate to it from the ID.
Ok got it .
TextInput::make('total')
->placeholder(function (Get $get,Set $set) {
$ser = $get('service_code');
$panell = $get('Panel');
$codes = []; $allcharges = []; $sumcharges=[];
if ($ser !== null && $panell !== null) { $codes = Service::whereIn('id', $ser) ->pluck('code') ->toArray();
$charges = Service::where('panel_name', $panell) ->whereIn('code', $codes) ->pluck('charges') ->toArray(); $allcharges = array_merge($allcharges, $charges); $sumcharges=array_sum($allcharges);
} $set('total', $sumcharges); }) ->reactive() ->live(), it is not working on edit page it show empty field @toeknee
$codes = []; $allcharges = []; $sumcharges=[];
if ($ser !== null && $panell !== null) { $codes = Service::whereIn('id', $ser) ->pluck('code') ->toArray();
$charges = Service::where('panel_name', $panell) ->whereIn('code', $codes) ->pluck('charges') ->toArray(); $allcharges = array_merge($allcharges, $charges); $sumcharges=array_sum($allcharges);
} $set('total', $sumcharges); }) ->reactive() ->live(), it is not working on edit page it show empty field @toeknee
This sin't making sense, you are using a placeholder to set the same field... I think what you want is a formatState ? and return the sumCharges?
I have solved it 🚀
Please provide your solution for others.
$ser = $get('service_code');
$panell = $get('Panel');
filed must be stored in database in order to calculate the sumcharges
i was not storing them.