Generating unique URL slugs in Laravel

In one of the projects I have been working upon, I decided to use slugs for URLs, instead of IDs because it allowed me to generate user-friendly URLs and to improve the SEO of the content.

In my case, there were two places where I decided to do this i.e.

Below is the super simple function that I used, which you can adapt to your own needs.

function generateSlug( $tipTitle ) {

    $slug = Str::slug( $tipTitle );
    $slugs = $this->whereRaw("slug REGEXP '^{$slug}(-[0-9]*)?$'");

    if ($slugs->count() === 0) {
        return $slug;
    }

    // Get the last matching slug
    $lastSlug = $slugs->orderBy('slug', 'desc')->first()->slug;

    // Strip the number off of the last slug, if any
    $lastSlugNumber = intval(str_replace($slug . '-', '', $lastSlug));

    // Increment/append the counter and return the slug we generated
    return $slug . '-' . ($lastSlugNumber + 1);
}

What's happening here is self explanatory, however if you have any problems understanding, feel free to leave a comment below. Also, you should note that I have used MySQL in this case, check you RDBMS' manual for the implementation of regular expressions.

👋 Follow me on twitter for the updates.