How to create a unique slug for a post?

When saving a post of a similar title, The CMS doesn’t automatically add a unique ID for the slug. The same slug will be saved for the new post and the old post will not be accessible anymore. Is there a way to make slugs save as a unique slug name?

Yes there is.

In your model yo can declare the slug as unique

public $rules = [
        'slug' => 'required|unique:table_name,slug',
    ];

You should look into the Sluggable trait.

This will automatically append a digit to the slug if one already exists.

Sluggable: October v3 Docs

Sluggable: October v2 docs

1 Like

thank you both! I got it working using the sluggable trait! Cheers!

1 Like