Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static function getSlug($title, $model) {
- $slug = Str::slug($title);
- $slugCount = count( $model->whereRaw("url REGEXP '^{$slug}(-[0-9]*)?$'")->get() );
- return ($slugCount > 0) ? "{$slug}-{$slugCount}" : $slug;
- }
- /************************************************************************/
- /************************************************************************/
- /************************************************************************/
- /************************************************************************/
- public function setSlugAttribute($value) {
- $slug = Str::slug($value);
- $slugs = static::whereRaw("slug REGEXP '^{$slug}(-[0-9]*)?$'");
- if ($slugs->count() === 0) {
- return $slug;
- }
- // get reverse order and get first
- $lastSlugNumber = intval(str_replace($slug . '-', '', $slugs->orderBy('slug', 'desc')->first()->slug));
- return $slug . '-' . ($lastSlugNumber + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement