Update field state every time other field updated without using $set()
I want to make my field (e.g total) updated every time I change other fields (e.g tax, price, discount). Using $set on each field is quite complicated, is it possible to update it on the total field instead?
7 Replies
you can use $get() in the total field
what function should I use to set the value? tried to use ->formatStateUsing() but doesnt work
formatStateUsing
can you share the code
or try state
in case the total is not an actually column in your dbyeah, its not, its just form field to show information
This is the field that will modify the total field
And this is the total that should updated the value
state() still doesnt work
try to user placeholder() instead of state(), you can see my example project code
That's good alternative thanks, I'll try it
TextInput::make('total')
->placeholder(function(Get $get){
$ser = $get('service_code');
$panell = $get('Panel');
// Initialize an array to store the codes $codes = []; $allcharges = []; $sumcharges=0;
// Check if both $ser and $panell are not null if ($ser !== null && $panell !== null) { // Retrieve codes from the database based on $ser $codes = Service::whereIn('id', $ser) ->pluck('code') ->toArray();
// Implement the query to fetch charges based on $panell // Assuming you have a table named "charges" with columns "panel_name" and "charge" $charges = Service::where('panel_name', $panell) ->whereIn('code', $codes) ->pluck('charges') ->toArray();
// Insert charges into $allcharges array $allcharges = array_merge($allcharges, $charges); $sumcharges=array_sum($allcharges); }
// Print the extracted codes and charges using var_dump // var_dump($sumcharges);
return $sumcharges; }) ->live(), it is not saving the data in the database @Tin Modric
// Initialize an array to store the codes $codes = []; $allcharges = []; $sumcharges=0;
// Check if both $ser and $panell are not null if ($ser !== null && $panell !== null) { // Retrieve codes from the database based on $ser $codes = Service::whereIn('id', $ser) ->pluck('code') ->toArray();
// Implement the query to fetch charges based on $panell // Assuming you have a table named "charges" with columns "panel_name" and "charge" $charges = Service::where('panel_name', $panell) ->whereIn('code', $codes) ->pluck('charges') ->toArray();
// Insert charges into $allcharges array $allcharges = array_merge($allcharges, $charges); $sumcharges=array_sum($allcharges); }
// Print the extracted codes and charges using var_dump // var_dump($sumcharges);
return $sumcharges; }) ->live(), it is not saving the data in the database @Tin Modric