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
Lara Zeus
Lara Zeus10mo ago
you can use $get() in the total field
fikurimax
fikurimaxOP10mo ago
what function should I use to set the value? tried to use ->formatStateUsing() but doesnt work
Lara Zeus
Lara Zeus10mo ago
formatStateUsing can you share the code or try state in case the total is not an actually column in your db
fikurimax
fikurimaxOP10mo ago
yeah, its not, its just form field to show information This is the field that will modify the total field
Grid::make()
->schema([
Toggle::make('discount_toggle')
->label("Apply discount")
->live(),
TextInput::make('discount')
->label("Discount (%)")
->disabled(fn (\Filament\Forms\Get $get) => $get('discount_toggle') == false)
->required(fn (\Filament\Forms\Get $get) => $get('discount_toggle') == true)
->postfix('%')
->live()
->numeric()
->maxValue(100)
->minValue(1),
]),
Grid::make()
->schema([
Toggle::make('discount_toggle')
->label("Apply discount")
->live(),
TextInput::make('discount')
->label("Discount (%)")
->disabled(fn (\Filament\Forms\Get $get) => $get('discount_toggle') == false)
->required(fn (\Filament\Forms\Get $get) => $get('discount_toggle') == true)
->postfix('%')
->live()
->numeric()
->maxValue(100)
->minValue(1),
]),
And this is the total that should updated the value
TextInput::make('total')
->label("Total")
->disabled()
->required()
->live()
->state(function (TextInput $component, \Filament\Forms\Get $get) {
$billingPrice = $get('billing_price');
if ($billingPrice === null) {
return;
}

$billingDiscount = $get('discount');
if ($billingDiscount !== null) {
$billingPrice = $billingPrice - ($billingPrice * ($billingDiscount / 100));
}

$component->state(str()->formatCurrency($billingPrice));
}),
TextInput::make('total')
->label("Total")
->disabled()
->required()
->live()
->state(function (TextInput $component, \Filament\Forms\Get $get) {
$billingPrice = $get('billing_price');
if ($billingPrice === null) {
return;
}

$billingDiscount = $get('discount');
if ($billingDiscount !== null) {
$billingPrice = $billingPrice - ($billingPrice * ($billingDiscount / 100));
}

$component->state(str()->formatCurrency($billingPrice));
}),
state() still doesnt work
aliif
aliif10mo ago
try to user placeholder() instead of state(), you can see my example project code
fikurimax
fikurimaxOP10mo ago
That's good alternative thanks, I'll try it
hashim199
hashim1997mo ago
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
Want results from more Discord servers?
Add your server