How To Create Unique Slug In Laravel 9

Websolutionstuff | Sep-27-2022 | Categories : Laravel

In this article, we will see how to create a unique slug in laravel 9. A slug is the part of a URL that identifies a particular page on a website in an easy-to-read form. a slug can be very powerful for keyword SEO. A good slug can improve a user’s experience if it clearly states what the webpage is about. So, we will generate a unique slug in laravel 9.

In this example, we will create a slug based on the title. For example, if the user enters a title like the laravel 9 slug generator example then the slug can be automatically created or generated like the laravel-9-slug-generator-example.

Also, you can create a slug using the laravel Str::slug() helper function and slug() method.

So, let's see laravel 9 create a unique slug or create a unique slug in PHP.

app/Models/Post.php

<?php

namespace App\Models;

use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Post extends Model
{
    use HasFactory;

    protected $fillable = [
        'title', 'detail', 'slug'
    ];

    /**
     * Boot the model.
     */
    protected static function boot()
    {
        parent::boot();

        static::created(function ($post) {
            $post->slug = $post->createSlug($post->title);
            $post->save();
        });
    }

    /** 
     * create slug
     *
     * @return response()
     */
    private function createSlug($title)
    {
        if (static::whereSlug($slug = Str::slug($title))->exists()) {
            $max = static::whereTitle($title)->latest('id')->skip(1)->value('slug');

            if (is_numeric($max[-1])) {
                return preg_replace_callback('/(\d+)$/', function ($mathces) {
                    return $mathces[1] + 1;
                }, $max);
            }

            return "{$slug}-2";
        }

        return $slug;
    }
}

 

 

app/Http/Controllers/PostController

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    /**
     * create a new post
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $post = Post::create([
            "title" => "Laravel Slug Create"
        ]);

        dd($post);
    }
}

Output:

laravel-slug-create

laravel-slug-create-1

laravel-slug-create-2

 

Create Slug using Str::slug()

The Str::slug method generates a URL friendly "slug" from the given string.

use Illuminate\Support\Str;
 
$slug = Str::slug('Laravel 9 Slug Helper Function', '-');

Output:

laravel-9-slug-helper-function

 

 

Generate Slug using slug() Method

The slug method generates a URL friendly "slug" from the given string.

use Illuminate\Support\Str;
 
$slug = Str::of('Laravel 9 Slug Method Example')->slug('-');

Output:

laravel-9-slug-method-example

 


You might also like :

Recommended Post
Featured Post
Laravel 9 User Role and Permission
Laravel 9 User Role and Permis...

In this article, we will show you laravel 9 user role and permission with an example. here we will see how to set u...

Read More

Mar-02-2022

How To Create Unique Slug In Laravel 9
How To Create Unique Slug In L...

In this article, we will see how to create a unique slug in laravel 9. A slug is the part of a URL that i...

Read More

Sep-27-2022

How To Add Bootstrap 5 Modal In Laravel 10
How To Add Bootstrap 5 Modal I...

In this article, we will see how to add the bootstrap 5 modal in laravel 10. Here, we will learn about the bootstra...

Read More

Apr-12-2023

How To Get Selected Checkbox List Value In Jquery
How To Get Selected Checkbox L...

In this tutorial, I will explain you to how to get the selected checkbox value from a checkbox list in jquery, If y...

Read More

Jun-17-2020