Laravel Create Unique Title Slugs for Blog Posts

Today, We want to share with you Laravel Create Unique Title Slugs for Blog Posts.In this post we will show you How to create a unique slug or unique url in Laravel, hear for Laravel 5.7 – How to create SEO friendly sluggable URL we will give you demo and example for implement.In this post, we will learn about The Easiest Way to Create Unique Slugs for Blog Posts in Laravel 5.7 with an example.

Laravel Create Unique Title Slugs for Blog Posts

There are the Following The simple About Laravel Create Unique Title Slugs for Blog Posts Full Information With Example and source code.

As I will cover this Post with live Working example to develop Creating Unique Title Slugs with Laravel, so the Creating Unique Title Slugs with Laravel for this example is following below.

When storing Data content in the MySQL database a classic pattern for getting it is to make a unique Based “slug” based on the main title. This gives users as well as any search engines a good and human-friendly Best way to access it through the Uniq Website URL.

For example, a classic URL might look like(Generate a Unique slug From Title), The id in the URL gives no hints at what the Data content is about and is not user-friendly. Slugs, on the other hand, are designed to turn this into

Make slugs from an in database string is easy in Laravel as it added a Laravel custom helper for just this use case:

$title = str_slug('Creating Unique Title Slugs with Laravel', '-');

The Data results of this would be a lower-cased string with a dash (-) separator between the words:



While this is a huge help it doesn’t account for situations where We have two small pieces of Data content with the same to same title.

So, Lets start a step by step build(Laravel unique url generator) a small utility class to handle making unique slugs based on existing data therefor we can ensure that no two are the same.

Creating Unique Title Slugs with Laravel

For reference here is the full Laravel Based class:

Laravel slug class

getRelatedSlugs($slug, $id);
        if (! $createSlugsAll->contains('slug', $slug)){
            return $slug;
        for ($i = 1; $i <= 10; $i++) {
            $makeSlug = $slug.'-'.$i;
            if (! $createSlugsAll->contains('slug', $makeSlug)) {
                return $makeSlug;
        throw new \Exception('Can not generate a unique slug');
    protected function getRelatedSlugs($slug, $id = 0)
        return article::select('slug')->where('slug', 'like', $slug.'%')
            ->where('id', '<>', $id)

What this does is give you a generateArticleslug method that can be used in making as well as editing by passing Laravel the existing data record id.

Article is an example of generating for both

On create Article

$article->slug = $slug->generateArticleslug($request->title);

On update Article

if ($article->slug != $request->slug) {
 $article->slug = $slug->generateArticleslug($request->slug, $id);

The Slug class itself is attractive simple. generateArticleslug calls getRelatedSlugs which performs a single Website query selecting all the data records that can possibly be a same to same. After that uses the Laravel Data object Collections class to display if it’s already used:

if (! $createSlugsAll->contains('slug', $slug)){
 return $slug;

If it still has a same to same then it appends a number to the end and performs another check for website Title uniqueness:

for ($i = 1; $i <= 10; $i++) {
    $makeSlug = $slug.’-’.$i;
    if (! $createSlugsAll->contains(‘slug’, $makeSlug)) {
        return $makeSlug;

Last words for this post, if all the numbers are allowed exhausted it simple data bails out by throwing Laravel PHP throw an Exception.

By utilizing Laravel’s existing main Laravel custom helpers as well as all the Collections creating unique title slugs is best and easy. With this step by step help to understand all that would be main required next is a custom Laravel route as well as a simple MySQL query to pull out a single article by its Web site Title slug:

Route::get('/article/{slug}', function(){
    $article = \App\article::where('slug', $slug)->firstOrFail(); 
Angular 6 CRUD Operations Application Tutorials

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Create Unique Title Slugs for Blog Posts.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment