Laravel Create Unique Title Slugs for Blog Posts – Technology

Laravel Create Unique Title Slugs for Blog Posts – Technology

In this post we will give you information about Laravel Create Unique Title Slugs for Blog Posts – Technology. Hear we will give you detail about Laravel Create Unique Title Slugs for Blog Posts – TechnologyAnd how to use it also give you demo for it if it is necessary.

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.

Another must read:  PHP Future

For example, a classic URL might look like(Generate a Unique slug From Title), onlinecode/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 onlinecode/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:

Another must read:  Vue js Laravel 5.8 File Upload Tutorial

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Laravel slug class

<?php
namespace AppServices;
use Apparticle;
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.

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 = Apparticle::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 onlinecode 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.

Hope this code and post will helped you for implement Laravel Create Unique Title Slugs for Blog Posts – Technology. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

17  +    =  21

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US