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.

Also Read :  PHP Creating Unique Title Slugs in Laravel

For example, a classic URL might look like(Generate a Unique slug From Title), pakainfo.com/article/1. 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 pakainfo.com/article/my-Best-Web-article

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:

output

creating-unique-title-slugs-with-laravel

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

<?php
namespace App\Services;
use App\article;
class Slug
{
    public function generateArticleslug($title, $id = 0)
    {
        $slug = str_slug($title);
        $createSlugsAll = $this->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)
            ->get();
    }
}

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.

Also Read :  Add prefix in each key of PHP Array

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.

Also Read :  Data Tables State save local storage in jQuery

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 :

Summary

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 Pakainfo.com 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 Reply

avatar
  Subscribe  
Notify of