Skip to content

Instantly share code, notes, and snippets.

@marifuli
Created March 30, 2022 00:51
Show Gist options
  • Save marifuli/d25b3c840647e49ed04c26b1f7b06da5 to your computer and use it in GitHub Desktop.
Save marifuli/d25b3c840647e49ed04c26b1f7b06da5 to your computer and use it in GitHub Desktop.
function generate_slug($modelClass, $modelColumnName, $title)
{
$slug = \Illuminate\Support\Str::slug($title);
$slugCount = count( $modelClass::whereRaw($modelColumnName . " REGEXP '^{$slug}(-[0-9]*)?$'")->get() );
return ($slugCount > 0) ? "{$slug}-{$slugCount}" : $slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment