There are sluggable packages on Packagist that can do this for you, but if you wish to roll your own, have a look at the code used in the Forum series, and the discussion around different gotchas here
Test field unicity with formRequest on function boot
Hi,
I'm trying to generate a slug with my Post title, using boot model function with static::creating. It works fine while it's unique. But if this duplicates existing slug, i cant' achieve to pass the test of my NewsFormRequest in the store resource method, and i'm getting the Integrity constraint violation: 1062. I guess i'm missing something logic, don't know what...
Thanks in advance ! Julien
Your FormRequest should be validating only the form input that came from the user; your user is not sending the slug, it is being determined by your app.
So, you do not need to validate a slug, because the slug is not in the Request object
Please or to participate in this conversation.