Skip to content

Instantly share code, notes, and snippets.

@brunocmoraes
Created August 11, 2020 14:50
Show Gist options
  • Save brunocmoraes/b20e2d5295ea62bceeb084bd6a18522f to your computer and use it in GitHub Desktop.
Save brunocmoraes/b20e2d5295ea62bceeb084bd6a18522f to your computer and use it in GitHub Desktop.
Append a slug number if aleardy exists on db
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