Created
March 18, 2015 10:40
-
-
Save ClaudioVarandas/5b5251c177abce78e223 to your computer and use it in GitHub Desktop.
Generate unique slugs in Laravel 4 (updated)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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