Automatically set the slug for new records only
For models with slugs, often you don't want the slug to (automatically) change after the model has been created, to ensure that URLs don't become broken.
To do that, I use the following:
TextInput::make('title') ->required() ->reactive() ->afterStateUpdated(function (Closure $set, $state, $context) { if ($context === 'edit') { return; } $set('slug', Str::slug($state)); }), TextInput::make('slug') ->required() ->maxLength(255) ->rules(['alpha_dash']) ->unique(ignoreRecord: true),
With thanks to Dan Harrin for the steer.
I have created a Filament Form plugin that addresses exactly this problem described above and also added some other nice features regarding title and slug creation.
"Title with Slug" Input - Simple Permalink Slugs for Filament Forms
https://filamentphp.com/plugins/title-with-slug-permalink
Simply add the Field to your Filament form, and all title and slug related editing functions are available.
Best, Andreas