src/Services/SlugService.php
protected function makeSlugUnique(string $slug, array $config, string $attribute): string
    {
        if (!$config['unique']) {
            return $slug;
        }

        $separator = $config['separator'];

        // find all models where the slug is like the current one
        $list = $this->getExistingSlugs($slug, $attribute, $config);

        // if ...
        // 	a) the list is empty, or
        // 	b) our slug isn't in the list
        // ... we are okay
        if (
            $list->count() === 0 ||
            $list->contains($slug) === false
        ) {
            return $slug;
        }

        // if our slug is in the list, but
        // 	a) it's for our model, or
        //  b) it looks like a suffixed version of our slug
        // ... we are also okay (use the current slug)
        if ($list->has($this->model->getKey())) {
            $currentSlug = $list->get($this->model->getKey());

            if (
                $currentSlug === $slug ||
                strpos($currentSlug, $slug) === 0
            ) {
                return $currentSlug;
            }
        }

        $method = $config['uniqueSuffix'];
        if ($method === null) {
            $suffix = $this->generateSuffix($slug, $separator, $list);
        } elseif (is_callable($method)) {
            $suffix = $method($slug, $separator, $list);
        } else {
            throw new \UnexpectedValueException('Sluggable "uniqueSuffix" for ' . get_class($this->model) . ':' . $attribute . ' is not null, or a closure.');
        }

        return $slug . $separator . $suffix;
    }
protected function makeSlugUnique(string $slug, array $config, string $attribute): string
    {
        if (!$config['unique']) {
            return $slug;
        }

        $separator = $config['separator'];

        // find all models where the slug is like the current one
        $list = $this->getExistingSlugs($slug, $attribute, $config);

        // if ...
        // 	a) the list is empty, or
        // 	b) our slug isn't in the list
        // ... we are okay
        if (
            $list->count() === 0 ||
            $list->contains($slug) === false
        ) {
            return $slug;
        }

        // if our slug is in the list, but
        // 	a) it's for our model, or
        //  b) it looks like a suffixed version of our slug
        // ... we are also okay (use the current slug)
        if ($list->has($this->model->getKey())) {
            $currentSlug = $list->get($this->model->getKey());

            if (
                $currentSlug === $slug ||
                strpos($currentSlug, $slug) === 0
            ) {
                return $currentSlug;
            }
        }

        $method = $config['uniqueSuffix'];
        if ($method === null) {
            $suffix = $this->generateSuffix($slug, $separator, $list);
        } elseif (is_callable($method)) {
            $suffix = $method($slug, $separator, $list);
        } else {
            throw new \UnexpectedValueException('Sluggable "uniqueSuffix" for ' . get_class($this->model) . ':' . $attribute . ' is not null, or a closure.');
        }

        return $slug . $separator . $suffix;
    }
protected function makeSlugUnique ( string $ slug , array $ config , string $ attribute ) : string { if ( ! $ config [ 'unique' ] ) { return $ slug ; } $ separator = $ config [ 'separator' ] ; // find all models where the slug is like the current one $ list = $ this -> getExistingSlugs ( $ slug , $ attribute , $ config ) ; // if ... // 	a) the list is empty, or // 	b) our slug isn't in the list // ... we are okay if ( $ list -> count ( ) === 0 || $ list -> contains ( $ slug ) === false ) { return $ slug ; } // if our slug is in the list, but // 	a) it's for our model, or //  b) it looks like a suffixed version of our slug // ... we are also okay (use the current slug) if ( $ list -> has ( $ this -> model -> getKey ( ) ) ) { $ currentSlug = $ list -> get ( $ this -> model -> getKey ( ) ) ; if ( $ currentSlug === $ slug || strpos ( $ currentSlug , $ slug ) === 0 ) { return $ currentSlug ; } } $ method = $ config [ 'uniqueSuffix' ] ; if ( $ method === null ) { $ suffix = $ this -> generateSuffix ( $ slug , $ separator , $ list ) ; } elseif ( is_callable ( $ method ) ) { $ suffix = $ method ( $ slug , $ separator , $ list ) ; } else { throw new \ UnexpectedValueException ( 'Sluggable "uniqueSuffix" for ' . get_class ( $ this -> model ) . ':' . $ attribute . ' is not null, or a closure.' ) ; } return $ slug . $ separator . $ suffix ; }
Checks if the slug should be unique, and makes it so if needed.

@param string $slug
@param array $config
@param string $attribute

@return string
@throws \UnexpectedValueException
Checks if the slug should be unique and makes it so if needed .