Tuning the uniqueness suffix | laravel-sluggable | Spatie

 SPATIE

  Laravel Sluggable
====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-sluggable](https://spatie.be/docs/laravel-sluggable/v4)  Advanced-usage  Tuning the uniqueness suffix

 Version   v4

 Other versions for crawler [v4](https://spatie.be/docs/laravel-sluggable/v4)

  Tuning the uniqueness suffix
- [ Introduction ](https://spatie.be/docs/laravel-sluggable/v4/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-sluggable/v4/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-sluggable/v4/installation-setup)
- [ Translatable slugs ](https://spatie.be/docs/laravel-sluggable/v4/translatable-slugs)
- [ Laravel Boost skill ](https://spatie.be/docs/laravel-sluggable/v4/laravel-boost-skill)
- [ Changelog ](https://spatie.be/docs/laravel-sluggable/v4/changelog)
- [ Questions and issues ](https://spatie.be/docs/laravel-sluggable/v4/questions-issues)
- [ Support us ](https://spatie.be/docs/laravel-sluggable/v4/support-us)
- [ Upgrading ](https://spatie.be/docs/laravel-sluggable/v4/upgrading)

Basic usage
-----------

- [ Generating your first slug ](https://spatie.be/docs/laravel-sluggable/v4/basic-usage/getting-started)
- [ Using the Sluggable attribute ](https://spatie.be/docs/laravel-sluggable/v4/basic-usage/using-the-attribute)
- [ Using the HasSlug trait ](https://spatie.be/docs/laravel-sluggable/v4/basic-usage/using-the-has-slug-trait)
- [ Finding models by slug ](https://spatie.be/docs/laravel-sluggable/v4/basic-usage/finding-models-by-slug)
- [ Self-healing URLs ](https://spatie.be/docs/laravel-sluggable/v4/basic-usage/self-healing-urls)

Advanced usage
--------------

- [ Combining multiple source columns ](https://spatie.be/docs/laravel-sluggable/v4/advanced-usage/source-fields)
- [ Tuning the uniqueness suffix ](https://spatie.be/docs/laravel-sluggable/v4/advanced-usage/uniqueness)
- [ Overriding the underlying actions ](https://spatie.be/docs/laravel-sluggable/v4/advanced-usage/overriding-actions)

 Tuning the uniqueness suffix
============================

###  On this page

1. [ Starting the counter at a different number ](#content-starting-the-counter-at-a-different-number)
2. [ Always append a suffix, even on the first record ](#content-always-append-a-suffix-even-on-the-first-record)

Slugs are unique by default. On a collision, the package appends a counter starting at `1` (for example `this-is-an-example`, `this-is-an-example-1`, `this-is-an-example-2`). Two builder methods change how that suffix is generated. They live on `SlugOptions` and are not available through the attribute.

Starting the counter at a different number
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

`startSlugSuffixFrom()` sets the first numeric suffix used on a collision.

```
SlugOptions::create()
    ->generateSlugsFrom('name')
    ->saveSlugsTo('slug')
    ->startSlugSuffixFrom(2);
```

Always append a suffix, even on the first record
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

`useSuffixOnFirstOccurrence()` adds a suffix even when the slug would otherwise be unique. Instead of `this-is-an-example`, `this-is-an-example-1`, you get `this-is-an-example-1`, `this-is-an-example-2`.

```
SlugOptions::create()
    ->generateSlugsFrom('name')
    ->saveSlugsTo('slug')
    ->useSuffixOnFirstOccurrence();
```

For a non-numeric or otherwise custom suffix, use the trait's `usingSuffixGenerator()`. See [A custom suffix generator](/docs/laravel-sluggable/v4/basic-usage/using-the-has-slug-trait#a-custom-suffix-generator).

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
