Skip to content

Instantly share code, notes, and snippets.

@jlpellicer
Last active December 20, 2023 16:57
Show Gist options
  • Save jlpellicer/e90a3b6fbdb173e03e1348761de9afd2 to your computer and use it in GitHub Desktop.
Save jlpellicer/e90a3b6fbdb173e03e1348761de9afd2 to your computer and use it in GitHub Desktop.
How to fill a default value in an text input in the edit page in Filament

Laravel 10 / Filament 3 / Livewire

When you open an edit form in Filament, the default property doesn't work, it only works when creating a new record.

In this case you want to use the afterStateHydrated property, inject the current form Component and, in my case, also inject the Get so that I can get the value of another property:

Forms\Components\TextInput::make('value')
    ->label(
        function(Get $get) {
            // setting the label to the name of a different property
            return $get('name') . ($get('unit') ? ' (' . $get('unit') . ')' : '');
        }
    )
    ->afterStateHydrated(
        function (Component $component, Get $get) {
            // here I can `Get` the value of some property and set the `state` of the `Component`
            $component->state($get('default_value'));
        }
    )

Bonus: setting the label name to a different value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment