This doesn't really have much to do with Laravel 5 routing but rather slugs.
Seeing Jeffrey is using slugs in the forum links, what happens if there are two threads with the same slug?
As you may have noticed, there already exists a thread with this same name, which should result in the same slug I suppose.
I am gonna submit this post/thread now and see what happens...
Edit: Oh, smart enough, it added -1 to the slug.
Now... I am curious how would you implement this in your app?
@JarekTkaczyk i think like will return too many results, maybe using a function to trim the last "-1".
or maybe using a foreach to check? for example it'll first check if the "l5-routing" is exist if yes, append -1 to it
then check if "l5-routing-1" exist if yes, append "-2" to it. i think there will not be too many topics with the same slug, so two or 3 queries are fine.
@RachidLaasri You could still do it with one query, regardless.
If the id field is auto-incrementing, then just do a like query, order by id in descending order and also limit to one result. If you get no results, you can insert slug as-is. If there are results, you get the last result and just make +1 to it.
But I get error "Invalid argument: arguments: (too few)". I find out in ->whereRaw() question mark was interpreted as placeholder for sql, not as optional param in REGEXP. How to fix it?
Having said that, I was having problems with the ? placeholder, because the query builder was confusing the second ? in the regex. So I did the following:
if ($count = Product::where('slug', 'like', "$slug%")->count())
$slug = str_finish($slug, "-$count");