Skip to content

Instantly share code, notes, and snippets.

@morshedx
Created June 28, 2019 05:05
Show Gist options
  • Save morshedx/25603ca3af5fd2562eb231ca6d3cb7c4 to your computer and use it in GitHub Desktop.
Save morshedx/25603ca3af5fd2562eb231ca6d3cb7c4 to your computer and use it in GitHub Desktop.
Create a unique slug from title
/**
* Create a unique slug from title.
*
* @link https://stackoverflow.com/questions/38800581/laravel-str-slug-not-working-for-unicode-bangla/38800717
* @link https://laracasts.com/discuss/channels/general-discussion/l5-routing-1
* @param string $title
* @return string
*/
public function makeSlugFromTitle($title)
{
$slug = preg_replace('/\s+/u', '-', trim($title));
$count = Category::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count();
return $count ? "{$slug}-{$count}" : $slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment