Generate slugs from a title whilst typing (without overriding)

Jun 8, 2022
Ralph J. Smit
Form builder

Sometimes you want your users to input a title (e.g. for a blog post) and automatically generate a slug from that title. However, you want to stop overriding the slug when the user has made at least one manual change.

Luckily, this is quite easy to achieve with a hidden field, to store an addition value:

->afterStateUpdated(function (Closure $get, Closure $set, ?string $state) {
if (! $get('is_slug_changed_manually') && filled($state)) {
$set('slug', Str::slug($state));
->afterStateUpdated(function (Closure $set) {
$set('is_slug_changed_manually', true);

No comments yet…