kirkbushell / eloquence

A drop-in library for certain database functionality in Laravel, that allows for extra features that may never make it into the main project.
MIT License
537 stars 58 forks source link

Ensure slug is unique with quick exists db check #66

Closed valorin closed 7 years ago

valorin commented 7 years ago

Since the Slug::fromId() algorithm doesn't produce unique slugs, this adds in a uniqueness check that will attempt to generate a new random slug instead.

I've added in a limit of 10 attempts, to avoid a potential 100+ loop slug search, which would slow down the process a fair bit. I think it's better to notify the developer that there are issues with slugs - which should only happen for a really large dataset.

I added the Slug::random() helper to generate truly random slug values. I based it off the Laravel str_random() code, with the additional limitation of only a-zA-Z.

It would be good to refactor fromId() out, as all it does is generate random slugs that aren't linked to id anyway. Also the use of md5() and uniqid() make it relatively insecure via brute-force guesswork.