F
Filament7mo ago
Emaz

Setting/getting nested data form wide

Am I doing this wrong? Over complicating? I found it pretty challenging to do calculations using form data that includes both root level and nested data . I finally came up with this scheme but it feels complex. Am I missing something? calcTotal is the nethod to recalculate and is triggered all over the palce from various root level and nested fields. glass_type is a root selector, so I pass 0 as the 1st argument to indicate no nesting. Since hingecuts is one schema down, I pass a 1 to indicate the nesting level. The calculation code must also account for the dot notation parent name, fabrication. hingecuts but this depends on where the calculation is called from. This works but it's pretty complex. Is this how it's done? My form has LOTS of this nesting and any field can trigger a recalculation that needs all the data.
Select::make('glass_type')
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get) {
static::calcTotal(0, $set, $get, "glass_type");
}),

Grid::make(6)
->relationship('fabrication')
->schema([
TextInput::make('hingecuts')->numeric()->label("Hinges")
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get) {
static::calcTotal(1, $set, $get, "hingecuts");
})


public static function calcTotal($nestUp, $set, $get, $calledFrom = null)
{

$path = $nestUp > 0 ? str_repeat('../', $nestUp) : '';

// Fetch root-level fields using the conditional path
$glass_type = $get($path . "glass_type");
$glass_options = $get($path . "glass_options");
$panels = $get($path . "panels"); // root level repeater
$hardware = $get($path . "hardware"); // root level repeater

$hingecuts = $get($calledFrom === 'hingecuts' ? "hingecuts" : $path . "fabrication.hingecuts");

// etc
Select::make('glass_type')
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get) {
static::calcTotal(0, $set, $get, "glass_type");
}),

Grid::make(6)
->relationship('fabrication')
->schema([
TextInput::make('hingecuts')->numeric()->label("Hinges")
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get) {
static::calcTotal(1, $set, $get, "hingecuts");
})


public static function calcTotal($nestUp, $set, $get, $calledFrom = null)
{

$path = $nestUp > 0 ? str_repeat('../', $nestUp) : '';

// Fetch root-level fields using the conditional path
$glass_type = $get($path . "glass_type");
$glass_options = $get($path . "glass_options");
$panels = $get($path . "panels"); // root level repeater
$hardware = $get($path . "hardware"); // root level repeater

$hingecuts = $get($calledFrom === 'hingecuts' ? "hingecuts" : $path . "fabrication.hingecuts");

// etc
No description
Solution:
dont store the total, have it computed based on the other fields?
Jump to solution
7 Replies
Emaz
EmazOP7mo ago
I'm kind of feeling like I over-complicated this. If not, what is everyone else doing? Can we simplify this in V4? Is nobody else doing this kind of thing? There must be an easier way. @Dan Harrin ?
Solution
Dan Harrin
Dan Harrin7mo ago
dont store the total, have it computed based on the other fields?
Dan Harrin
Dan Harrin7mo ago
with a placeholder instead of $set its up to you
Emaz
EmazOP7mo ago
Oh good point. I am already using a placeholder. So you mean move all the totaling over there in once place. I guess that simplifies things because now the nesting will be all from one place? So the gets that need "../../" will at least be consistent. So I DID over complicate by doing the calculations at each field that changes, thus creating the nesting that varies. Better to do it all at the placeholder element. That's what you're saying, right?
Dan Harrin
Dan Harrin7mo ago
yeah, as long as the fields are live they will rerender the placeholder content
Emaz
EmazOP7mo ago
I KNEW I was doing it wrong LOL. Thank you Dan. This would make a great tutorial or video series - I wish I had the time to do one. I just suspect others might make the same mistake in doing the calculations. My initial thought was to do it right at the field that changed. That can get real complicated real fast.
blex
blex6mo ago
Hey @Emaz would you mind showcasing your placeholder solution please? I'm trying to do the exact same thing and currently utalising a similar nesting function to determine the $get scope correctly which is getting too complicated Oh was actually really simple, ended up doing this which is far cleaner than nested scope gets per field Placeholder::make('overview') ->hiddenLabel() ->content(function(Get $get){ $data = self::updateGrandTotal($get); return view('forms.components.prod-quote-calculations')->with('data', $data); })
Cheers for the insight!
Want results from more Discord servers?
Add your server