Skip to content

Instantly share code, notes, and snippets.

@nickclasener
Created August 7, 2017 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickclasener/c823f806e653900f35d69c443be5a405 to your computer and use it in GitHub Desktop.
Save nickclasener/c823f806e653900f35d69c443be5a405 to your computer and use it in GitHub Desktop.
public static function boot()
{
parent::boot();
static::creating(function($model) {
$model->slug = str_slug($model->ToBeSlugified);
$latestSlug =
static::whereRaw("slug = '$model->slug' or slug LIKE '$model->slug-%'")
->latest('id')
->value('slug');
if ($latestSlug) {
$pieces = explode('-', $latestSlug);
$number = intval(end($pieces));
$model->slug .= '-' . ($number + 1);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment