Automatically set the slug for new records only

Jul 20, 2022
Admin panel, Form builder

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:

->afterStateUpdated(function (Closure $set, $state, $context) {
if ($context === 'edit') {
$set('slug', Str::slug($state));
->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

Simply add the Field to your Filament form, and all title and slug related editing functions are available.

fieldTitle: 'title',
fieldSlug: 'slug',

Best, Andreas