Is it somehow possible to access the value of a different field?

I am trying to use the value of a different field as label for another one. I tried it like so:
RepeatableEntry::make('listing.operating_expenses')
->label('Operating Expenses')
->schema([
TextEntry::make('operating_expenses_name')
->hiddenLabel(true),

TextEntry::make('operating_expenses_cost')
->label(
fn($value) => $value['operating_expenses_name'] ?? 'Operating Expenses'
)
->inlineLabel(true)
->money('AED'),
])->contained(false),
RepeatableEntry::make('listing.operating_expenses')
->label('Operating Expenses')
->schema([
TextEntry::make('operating_expenses_name')
->hiddenLabel(true),

TextEntry::make('operating_expenses_cost')
->label(
fn($value) => $value['operating_expenses_name'] ?? 'Operating Expenses'
)
->inlineLabel(true)
->money('AED'),
])->contained(false),
But that seems not work. Help is much appreciated πŸ™‚
6 Replies
Dennis Koch
Dennis Kochβ€’7mo ago
Yes. Use fn ($record) Not sure which one it is in repeatable though. πŸ€”
TheNastyPasty
TheNastyPastyOPβ€’7mo ago
Thanks, but if I use $record I dont have access to the operating_expenses array I need to access the array in the specific iteration to get the corresponding name for the cost
Bruno Pereira
Bruno Pereiraβ€’7mo ago
Can't you just pass Callable $get and use $get('operating_expenses_name') ?
TheNastyPasty
TheNastyPastyOPβ€’7mo ago
If I do so I get
Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization
Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization
TextEntry::make('operating_expenses_cost')
->label(function (Get $get) {
return $get('operating_expenses_name');
})
->inlineLabel(true)
->money('AED'),
TextEntry::make('operating_expenses_cost')
->label(function (Get $get) {
return $get('operating_expenses_name');
})
->inlineLabel(true)
->money('AED'),
Bruno Pereira
Bruno Pereiraβ€’7mo ago
Oh this is infolists. Ok that changes a bit.
Components\ImageEntry::make('photo')
->label(function ($component) {
$index = explode('.', $component->getStatePath())[1];
$data = $component
->getContainer()
->getParentComponent()
->getState()[$index];

return $data['first'] . ' ' . $data['last'];
}),
Components\ImageEntry::make('photo')
->label(function ($component) {
$index = explode('.', $component->getStatePath())[1];
$data = $component
->getContainer()
->getParentComponent()
->getState()[$index];

return $data['first'] . ' ' . $data['last'];
}),
Saw this on a discussion on the filament repo, you can trial and error to access the Model $record relation with it.
TheNastyPasty
TheNastyPastyOPβ€’7mo ago
Thanks I will try it πŸ™‚ Somehow the index does here not increase
$index = explode('.', $component->getStatePath())[1];
$data = $component
->getContainer()
->getParentComponent()
->getState()[$index];

return $data['operating_expenses_name'];
$index = explode('.', $component->getStatePath())[1];
$data = $component
->getContainer()
->getParentComponent()
->getState()[$index];

return $data['operating_expenses_name'];
Want results from more Discord servers?
Add your server