How to Generate Slugs from a Title in Filament
In this lesson, we will see how to generate slugs from a title in Filament, Sometimes we need to generate a slug automatically from a given title, so how can we do that?
Generate a slug automatically from a given title
To do that, add the 'live' method to your title field to be sure that the slug is filled in real time when the title is typed.
And the 'afterStateUpdated' method generates a slug automatically from the typed title.
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255)
->live()
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
Forms\Components\TextInput::make('slug')
->required()
->maxLength(255),
]);
}