Created
August 7, 2017 11:57
-
-
Save nickclasener/c823f806e653900f35d69c443be5a405 to your computer and use it in GitHub Desktop.
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
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