Tuning the uniqueness suffix | laravel-sluggable-rust | Spatie       

 SPATIE  

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

spatie.be/open-source

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

 Version   v1      

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

  Tuning the uniqueness suffix    
- [ Introduction ](https://spatie.be/docs/laravel-sluggable-rust/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-sluggable-rust/v1/requirements)
- [ Support us ](https://spatie.be/docs/laravel-sluggable-rust/v1/support-us)
- [ Installation and setup ](https://spatie.be/docs/laravel-sluggable-rust/v1/installation-setup)
- [ Translatable slugs ](https://spatie.be/docs/laravel-sluggable-rust/v1/translatable-slugs)
- [ Changelog ](https://spatie.be/docs/laravel-sluggable-rust/v1/changelog)
- [ Questions and issues ](https://spatie.be/docs/laravel-sluggable-rust/v1/questions-issues)
- [ Upgrading ](https://spatie.be/docs/laravel-sluggable-rust/v1/upgrading)
- [ Testing ](https://spatie.be/docs/laravel-sluggable-rust/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-sluggable-rust/v1/releasing)

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

- [ Generating your first slug ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/getting-started)
- [ Using slug options ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/using-slug-options)
- [ Finding models by slug ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/finding-models-by-slug)
- [ Self-healing URLs ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/self-healing-urls)

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

- [ Combining multiple source columns ](https://spatie.be/docs/laravel-sluggable-rust/v1/advanced-usage/source-fields)
- [ Tuning the uniqueness suffix ](https://spatie.be/docs/laravel-sluggable-rust/v1/advanced-usage/uniqueness)
- [ Overriding the underlying actions ](https://spatie.be/docs/laravel-sluggable-rust/v1/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 ](#content-always-append-a-suffix)
3. [ A custom suffix generator ](#content-a-custom-suffix-generator)
4. [ Scope and concurrent writes ](#content-scope-and-concurrent-writes)

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`, and `this-is-an-example-2`.

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

`start_slug_suffix_from(2)` starts collisions at `-2`. Values below one are clamped to one. The package uses the first available suffix, so gaps can be reused.

Always append a suffix
--------------------------------------------------------------------------------------------------------------------------

`use_suffix_on_first_occurrence()` adds a suffix even when the base slug is available. Combine it with `start_slug_suffix_from` to choose the first number.

A custom suffix generator
-----------------------------------------------------------------------------------------------------------------------------------

`using_suffix_generator` receives the original slug and a zero-based collision iteration. The callback controls the suffix, not the separator. It runs once for each candidate that needs a suffix.

```
let options = Post::slug_options()
    .using_suffix_generator(|_slug, iteration| format!("edition{}", iteration + 1));
```

`maximum_suffix_attempts(100)` caps collision attempts. The default is 10,000, so a callback that keeps returning an occupied suffix fails rather than hanging a request. Errors stop the model write.

Scope and concurrent writes
-----------------------------------------------------------------------------------------------------------------------------------------

Uniqueness checks use exact equality, so `%`, `_`, and backslashes are not SQL wildcard patterns. The current row is excluded on updates. Other rows are checked without visibility filtering, including soft-deleted rows. Use `extra_scope` for tenant-scoped uniqueness.

A unique database index is still required when concurrent writes must not create duplicates. If two transactions choose the same slug, let the index reject one and retry the model write in a new transaction.

Uniqueness uses the model's configured connection, and updates retain the loaded model's connection. When creating through a query with a different runtime connection, set that same connection in `extra_scope(|query, _| query.connection("tenant"))`; Laravel's creation draft does not expose query connection overrides.

 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)
