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.
TextInput::make('vehicle_price')
->suffix('€')
->reactive(),

TextInput::make('down_payment')
->suffix('€')
->reactive(),

TextInput::make('loan_amount')
->suffix('€'),
TextInput::make('vehicle_price')
->suffix('€')
->reactive(),

TextInput::make('down_payment')
->suffix('€')
->reactive(),

TextInput::make('loan_amount')
->suffix('€'),
11 Replies
toeknee
toeknee2y ago
Use a closure, like
TextInput::make('vehicle_price')
->suffix('€')
->afterStateUpdated(fn($state, Closure $get, $set) => $set('loan_amount', (int)$state-(int)$get('down_payment')
->reactive(),

TextInput::make('down_payment')
->suffix('€')
->afterStateUpdated(fn($state, Closure $get, $set) => $set('loan_amount', (int)$state-(int)$get('down_payment')
->reactive(),

TextInput::make('loan_amount')
->suffix('€'),
TextInput::make('vehicle_price')
->suffix('€')
->afterStateUpdated(fn($state, Closure $get, $set) => $set('loan_amount', (int)$state-(int)$get('down_payment')
->reactive(),

TextInput::make('down_payment')
->suffix('€')
->afterStateUpdated(fn($state, Closure $get, $set) => $set('loan_amount', (int)$state-(int)$get('down_payment')
->reactive(),

TextInput::make('loan_amount')
->suffix('€'),
rajul
rajul2y ago
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?
toeknee
toeknee2y ago
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
rajul
rajul2y ago
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.
toeknee
toeknee2y ago
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.
rajul
rajul2y ago
Ok got it .
hashim199
hashim1997mo ago
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
toeknee
toeknee7mo ago
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?
hashim199
hashim1997mo ago
I have solved it 🚀
toeknee
toeknee7mo ago
Please provide your solution for others.
hashim199
hashim1997mo ago
$ser = $get('service_code'); $panell = $get('Panel'); filed must be stored in database in order to calculate the sumcharges i was not storing them.
Want results from more Discord servers?
Add your server