Are you using eloquent?
The actual method doesn't validate against any values by default.
// Illuminate/Support/helpers.php
if (! function_exists('str_slug')) {
/**
* Generate a URL friendly "slug" from a given string.
*
* @param string $title
* @param string $separator
* @return string
*/
function str_slug($title, $separator = '-')
{
return Str::slug($title, $separator);
}
}
So, you have a couple options:
-
Call
str_slug()then test the returned value against your database (perhaps start by defining your slug column as unique so the database enforces the constraint). At this point do as WordPress and append separator + 1 (i.e., my-new-slug-1). -
Go ahead and append a unique string to your slugs by default (perhaps datestamp or post UUID).