© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
4 replies
Kiran Timsina

Help accessing parent form field value

Since I could not $get the field value when trying to access the field value I found a hack to set form variables like this:

public static function getCakeFlavorsFormField(): Select
    {
        return Select::make('flavors_list')
            ->relationship('flavors', 'name') // We need this here since it must load the set data from pivot table
            ->multiple()
            ->options(Flavor::getFlavorsPluck('active')->toArray())
            ->preload()
            ->reactive()
            ->afterStateUpdated(function (callable $set, $state) {
                // $state is the selected flavors
                if (!$state) {
                    $flavors = Flavor::getFlavorsPluck('active')->toArray();
                } else {
                    $flavors = Flavor::getFlavorsPluck('active')->only($state)->toArray();
                }

                // Set 'flavors' for the 'cake' group
                $set('cake.selectedFlavors', $flavors);
                $set('cake.flavor_id', null); // Reset the default flavor whenever flavors_list changes
            })->required();
    }
    public static function getDefaultCakeFlavorFormField(): Group
    {
        return Group::make()->schema([
            Select::make('flavor_id')->label('Default Flavor')
                ->options(function (callable $get): array {

                    // I could not do $get('flavors_list') and get the values directly. This is always null

                    $flavors = $get('selectedFlavors');
                    if ($flavors) {
                        return $get('selectedFlavors');
                    }
                    return Flavor::getFlavorsPluck('active')->toArray();
                })
        ])->relationship('cake');
    }
public static function getCakeFlavorsFormField(): Select
    {
        return Select::make('flavors_list')
            ->relationship('flavors', 'name') // We need this here since it must load the set data from pivot table
            ->multiple()
            ->options(Flavor::getFlavorsPluck('active')->toArray())
            ->preload()
            ->reactive()
            ->afterStateUpdated(function (callable $set, $state) {
                // $state is the selected flavors
                if (!$state) {
                    $flavors = Flavor::getFlavorsPluck('active')->toArray();
                } else {
                    $flavors = Flavor::getFlavorsPluck('active')->only($state)->toArray();
                }

                // Set 'flavors' for the 'cake' group
                $set('cake.selectedFlavors', $flavors);
                $set('cake.flavor_id', null); // Reset the default flavor whenever flavors_list changes
            })->required();
    }
    public static function getDefaultCakeFlavorFormField(): Group
    {
        return Group::make()->schema([
            Select::make('flavor_id')->label('Default Flavor')
                ->options(function (callable $get): array {

                    // I could not do $get('flavors_list') and get the values directly. This is always null

                    $flavors = $get('selectedFlavors');
                    if ($flavors) {
                        return $get('selectedFlavors');
                    }
                    return Flavor::getFlavorsPluck('active')->toArray();
                })
        ])->relationship('cake');
    }


But a much easier way would be if I could simply $get the field value from another formgroup. Does anyone have idea about this?
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Setting Child value from Parent Form
FilamentFFilament / ❓┊help
2y ago
Accessing form values after saving
FilamentFFilament / ❓┊help
3y ago
Set form field value from outside the form
FilamentFFilament / ❓┊help
3y ago
Livewire form field not included in parent form's data
FilamentFFilament / ❓┊help
9mo ago