Skip to content

Instantly share code, notes, and snippets.

@ClaudioVarandas
Created March 18, 2015 10:40
Show Gist options
  • Save ClaudioVarandas/5b5251c177abce78e223 to your computer and use it in GitHub Desktop.
Save ClaudioVarandas/5b5251c177abce78e223 to your computer and use it in GitHub Desktop.
Generate unique slugs in Laravel 4 (updated)
function generateSlug($value)
{
$slug = \Str::slug($value);
$slugs = $this->whereRaw("slug REGEXP ? ", ["^{$slug}(-[0-9]*)?$"]);
$count = $slugs->count();
if ($count === 0) {
return $slug;
}
return $slug = $slug . '-' . ($count + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment