How to update field when is hidden

Hello, I have 2 select in a form. The second is only visible when a value of the first select has a understated. I'm having trouble when I update a status that includes a substate and change it to one that doesn't, the substate is not nulled on save in the database.

Forms\Components\Select::make('status_id')
    ->relationship('status', 'name', fn(Builder $query) => $query
        ->where('parent_id', 0)
        ->orderBy('order'))
    ->required()
    ->reactive()
    ->afterStateUpdated(fn ($set) => $set('understated_id', null))
    ->default(1)
    ->disabled(isCustomer()),

Forms\Components\Select::make('understated_id')
    ->options(function (\Closure $get) {
        $status = Status::query()->where('parent_id', '=', $get('status_id'))
            ->orderBy('order')->get();
        if ($status) {
            return $status->where('parent_id', $get('status_id'))->pluck('name', 'id');
        }
        return [];
    })
    ->visible(function (\Closure $get) {
        if ($get('status_id') <> '' && Status::query()->where('parent_id', '=', $get('status_id'))->exists()) {
            return true;
        }
        return false;
    })
    ->required(),
Was this page helpful?