Problem with afterStateHydrated

In a custom page, I am trying to set my toggle buttons default value based on its dynamic options. The options are based on different field values.

I have tried both default() and afterStateHydrated(). Even though I can populate the options correctly, I can not access the fields options in either of those methods when the options are dependent on other fields. How can I fix this?


Assume: $get('dob') = '02/20/2022' and $get('prematurity_id') = '02/30/2022'

This correctly gets the options
ToggleButton::make('questionnaire_id')
    ->options( fn (Get $get) =>
       self::getQuestionnaireOptions($get('dob'), $get('prematurity_id'))
    )
    ->inline()
    ->label(trans('Select Questionnaire'))
    ->required(),


Trying to set the default, but $component->getOptions() returns null
ToggleButton::make('questionnaire_id')
    ->options( fn (Get $get) =>
       self::getQuestionnaireOptions($get('dob'), $get('prematurity_id'))
    )
    ->afterStateHydrated(function (ToggleButton $component) {
       $component->state(array_key_first($component->getOptions()));
     })
    ->inline()
    ->label(trans('Select Questionnaire'))
    ->required(),


If I don't try to GET the value from other fields, this correctly sets the default
ToggleButton::make('questionnaire_id')
    ->options( fn (Get $get) =>
       self::getQuestionnaireOptions('02/20/2022', '02/30/2022')
    )
    ->afterStateHydrated(function (ToggleButton $component) {
       $component->state(array_key_first($component->getOptions()));
     })
    ->inline()
    ->label(trans('Select Questionnaire'))
    ->required(),
Was this page helpful?