Generate form fields based on relationship when it was changed

Generate form fields based on relationship when it was changed
Select::make('category_id')
->relationship('category', 'name')
->required()
->reactive()
->preload()
->searchable()
->afterStateUpdated(function (string $state) {
$attributeGroups = CategoryAttributeGroup::query()
->where('category_id', $state)
->isVisible()
->with('categoryAttributes')
->get();
}),
...
Select::make('category_id')
->relationship('category', 'name')
->required()
->reactive()
->preload()
->searchable()
->afterStateUpdated(function (string $state) {
$attributeGroups = CategoryAttributeGroup::query()
->where('category_id', $state)
->isVisible()
->with('categoryAttributes')
->get();
}),
...
When the category was changed, i want to get all the attributes relationship of this category and show on the form schema as a select field ($attributeGroups is a list of attribute groups that have many attributes in that group). Is Filament can do that and how? Thanks guys
6 Replies
ngoquocdat
ngoquocdat14mo ago
I trying this way but it doesn't works
public static function form(Form $form): Form
{
$attributeGroups = [];

return $form
->schema([
...$attributeGroups,
Select::make('category_id')
->relationship('category', 'name')
->required()
->reactive()
->preload()
->searchable()
->afterStateUpdated(function (string $state) use (&$attributeGroups) {
$groups = CategoryAttributeGroup::query()
->where('category_id', $state)
->isVisible()
->with('categoryAttributes')
->get();

$attributeGroups = $groups->map(function (CategoryAttributeGroup $group) {
return Select::make("attributes.{$group->id}");
})->toArray();
}),
public static function form(Form $form): Form
{
$attributeGroups = [];

return $form
->schema([
...$attributeGroups,
Select::make('category_id')
->relationship('category', 'name')
->required()
->reactive()
->preload()
->searchable()
->afterStateUpdated(function (string $state) use (&$attributeGroups) {
$groups = CategoryAttributeGroup::query()
->where('category_id', $state)
->isVisible()
->with('categoryAttributes')
->get();

$attributeGroups = $groups->map(function (CategoryAttributeGroup $group) {
return Select::make("attributes.{$group->id}");
})->toArray();
}),
Dan Harrin
Dan Harrin14mo ago
please search “dependant selects” on the docs
ngoquocdat
ngoquocdat14mo ago
Thank you, but it seems to only work with rendered fields. In my case: 1 category will have many attribute groups, and 1 group will have many attributes. So I want when the category is selected, will get all the groups of the category displayed as select field, and the attributes of that group will be the options of the select.
Dan Harrin
Dan Harrin14mo ago
you cannot use afterStateUpdated() to change configuration of other fields the way you would do it is by passing a function to the configuration methods instead, and using $get to get the other field value
ngoquocdat
ngoquocdat14mo ago
Does the docs mention this, or where can I find out about the configuration method. Thx ❤️😅
Dan Harrin
Dan Harrin14mo ago
read the Advanced section of the Form Builder docs