cviebrock / eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel
MIT License
3.91k stars 461 forks source link

Add ability to customize slug uniqueness resolution #611

Open viicslen opened 7 months ago

viicslen commented 7 months ago

This pull request introduces a new feature to allow for personalization of unique slugs. If the slug is not unique and you want to try to make it unique by replacing some other parts of the slug instead of adding a suffic, you can set the uniqueUsing configuration option to a closure which accepts the base slug, the separator, and a Collection of the other "similar" slugs as arguments. The closure should return the new unique slug to use, if the slug is still not unique, it will append a suffix to the returned slug.

Missing:

cviebrock commented 7 months ago

I like this idea ... but have a few thoughts:

Instead of making this a new configuration setting, we could just change the way the current unique setting works:

For something like this, I might extract all the existing code that handles suffixes into it's own class, and just make that the default value for unique, so we're always running a callable ... either the built-in one, or a custom one like you envisioned.

I'm also curious ... what was your use-case for this? And could we add some tests for your PR?

Thanks!