How to create a unique slug or unique url in Laravel From title - Laravel Easy Tutorials

Question:

How to create a unique slug or unique url in Laravel From title?

How to create a unique slug or unique url in Laravel From title - Laravel Easy Tutorials

Solution:

Suppose I've a Model Product and tables products. In that table, there is a slug and When I'll insert any product by product title, then I'll make a unique slug by using that title.


At first We know that how to create a slug in laravel. That's
str_slug($title, $separator="-");

If I've requested a title, then from that title I can make a slug by this -
public function store(Request $request)
    {
       $title = $request->title;
       $slug = str_slug($title, "-");
       //Rest of the works...
    }

But,
If we create a slug by this that's never be unique. Unique means -

  1. Add another same level title can create another slug. Like first title = 'my-title' and again I've a post titled by 'my-title' , then if I store it needs to store it like my-title-1 or my-title-2

We'll now create a slug that'll be totally unique.

Let's Start:

Create our own function that generate unique slug from any title. Look at the code and that is pretty much clear.

Full ProductController Class


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

use App\Product;

class ProductController extends Controller
{

    public function store(Request $request)
    {
        $product = new Product;
        $product->title = $request->title;

        //Make a unique product slug
        $product->slug = $this->createSlug($request->title);

        $product->save();
        return redirect('/');
    }


    //For Generating Unique Slug Our Custom function
    public function createSlug($title, $id = 0)
    {
        // Normalize the title
        $slug = str_slug($title);
        // Get any that could possibly be related.
        // This cuts the queries down by doing it once.
        $allSlugs = $this->getRelatedSlugs($slug, $id);
        // If we haven't used it before then we are all good.
        if (! $allSlugs->contains('slug', $slug)){
            return $slug;
        }
        // Just append numbers like a savage until we find not used.
        for ($i = 1; $i <= 10; $i++) {
            $newSlug = $slug.'-'.$i;
            if (! $allSlugs->contains('slug', $newSlug)) {
                return $newSlug;
            }
        }
        throw new \Exception('Can not create a unique slug');
    }

    protected function getRelatedSlugs($slug, $id = 0)
    {
        return Product::select('slug')->where('slug', 'like', $slug.'%')
        ->where('id', '<>', $id)
        ->get();
    }
}



That's it. We've created an Unique slug in Laravel.
So, If we give

  1. first title = 'Our Product' Then slug will be our our-product
  2. second title = 'Our Product' Then slug will be our-product-1
  3. third title = 'Our Product' Then slug will be our-product-2

So, the URL or Slug now is totally Unique. That's the system to create a unique slug in Laravel.



Tags:
How to create a unique slug or unique url in Laravel From title, Laravel create slug, Laravel create URL, How to create a unique slug in Laravel. Laravel Unique URL
How to create a unique slug or unique url in Laravel From title - Laravel Easy Tutorials How to create a unique slug or unique url in Laravel From title - Laravel Easy Tutorials Reviewed by Maniruzzaman Akash on November 04, 2017 Rating: 5

13 comments:

  1. Our certified Laravel developers can create scintillating extensions and mapping of web applications that ensure quicker performance. We have the acumen of delivering small scale to large scale projects based on laravel Development. You name the service that you are looking for in Laravel technology and we will be able to deliver that to you in minimal time. Contact@ExpressTechSoftwares.Com or +91-9806724185

    ReplyDelete
  2. Nice tutorial. Saved my time. Thanks

    ReplyDelete
  3. Very helpful article. According to Laraval usage statistics compiled by BuiltWith, Laravel has been steadily growing in popularity, with more than 700,000 live sites using it. In USA, laravel development companies are rapidly growing with its increasing demands.

    ReplyDelete

Powered by Blogger.