Created
August 11, 2020 14:50
-
-
Save brunocmoraes/b20e2d5295ea62bceeb084bd6a18522f to your computer and use it in GitHub Desktop.
Append a slug number if aleardy exists on db
This file contains hidden or 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 function setTitleAttribute($value) | |
{ | |
$slug = \Str::slug($value); | |
$count = static::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count(); | |
$this->attributes['title'] = $value; | |
$this->attributes['slug'] = $count ? "{$slug}-{$count}" : $slug; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment