/**
* Generate a unique slug.
* If it already exists, a number suffix will be appended.
* It probably works only with MySQL.
*
*
* @param Illuminate\Database\Eloquent\Model $model
* @param string $value
* @return string
*/
public static function getUniqueSlug($model, $value) {
$slug = \Illuminate\Support\Str::slug(trim($value));
$slugCount = count($model->whereRaw(“slug REGEXP ‘^{$slug}(-[0-9]+)?$’ and id != ‘{$model->id}'”)->get());
return ($slugCount > 0) ? “{$slug}-{$slugCount}” : $slug;
}
TO create Slug
I am create slug for cms Pages
Firstly Create Cms Model Object.
$cmsObj = new Cms();
$slug = BasicFunction::getUniqueSlug($cmsObj, $request->title);