F
Filament•8mo ago
Clear

Dynamic form for a products attributes

I want to display a form where the product attributes can be update. Example db schema:
products
id
slug
name

attributes
id
name
sort
weight


attribute_values
id
attribute_id
value
computed

products_attribute_values
id
attribute_id
value_id
comment
products
id
slug
name

attributes
id
name
sort
weight


attribute_values
id
attribute_id
value
computed

products_attribute_values
id
attribute_id
value_id
comment
I'm able to generate the fields using
foreach ($attributes as $attribute){
$fields[] = Forms\Components\TextInput::make('attributes.' . $attribute->id)
->label($attribute->name)->default('value!');
}
foreach ($attributes as $attribute){
$fields[] = Forms\Components\TextInput::make('attributes.' . $attribute->id)
->label($attribute->name)->default('value!');
}
But the I can't set the field value, instead of seeing "value!" the input field is empty.
1 Reply
🤖transistor🤖
One important thing is missing from your post, are you editing or creating a new record? Becuase the default property won't work on edit, only on create. If that's your case, you want to use the afterStateHydrated and inject your component and model record so you do something like this:
->afterStateHydrated(
function (Component $component, Model $record) {
$component->state($record->your_property);
}
)
->afterStateHydrated(
function (Component $component, Model $record) {
$component->state($record->your_property);
}
)
what throws me off is your loop, have you looked into Relation Managers? https://filamentphp.com/docs/3.x/panels/resources/relation-managers
Want results from more Discord servers?
Add your server