Skip to content

Instantly share code, notes, and snippets.

@aranw
Created October 1, 2012 09:54
Show Gist options
  • Save aranw/3810637 to your computer and use it in GitHub Desktop.
Save aranw/3810637 to your computer and use it in GitHub Desktop.
Unique Slug Code
public static function unique_slug($title)
{
$post_slug = \Str::slug($title);
$next_slug = null;
$i = 0;
do
{
if ($i == 0)
{
$post = static::where('slug', '=', $post_slug)->first();
}
else
{
$next_slug = $post_slug . "-{$i}";
$post = static::where('slug', '=', $next_slug)->first();
}
$i++;
}
while( !is_null( $post ));
return ($next_slug != null) ? $next_slug : $post_slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment