How to create unique slug while entering same title in Laravel 5.8

Viewed 300

I want to create unique slug for same title.

Example - www.xcv.com/ght-ghy/

Example 1 - www.xcv.com/ght-ghy-1/

Here is my controller code:

public function addPostDetails(Request $request) {
    $postTitle = $request->postTitle;
    $postDesc = $request->postDesc;
    $postCat = $request->postCat;
    $postTags = $request->tagid; 
    $tags = explode(",", $postTags);
    $postglobid = date('y') . date('s') . $postCat . rand(0000, 9999);
    $posturl = Str::slug($postTitle, '-');
    $galImg = array();
    $postimgnm = NULL;

    if($postimg = $request->hasFile('postWallImage')) {
        $postimg = $request->file('postWallImage');
        $postimgnm = $postimg->getClientOriginalName();
        $storeTo = public_path() . '/images/' . $postglobid;
        File::makeDirectory($storeTo, $mode = 0777, true, true);
        $postimg->move($storeTo, $postimgnm);
    }
    
    if($postimges = $request->hasFile('postImgGal')) {
        $postimges = $request->file('postImgGal');
        foreach($postimges as $imgs) {
            $postimgnms = $imgs->getClientOriginalName();
            $storeTo = public_path() . '/images/' . $postglobid . '/gallery/';
            File::makeDirectory($storeTo, $mode = 0777, true, true);
            $imgs->move($storeTo, $postimgnms);
            array_push($galImg, $postimgnms);
        }
    }
    
    $savecat = $this->savePostByCategory($postCat, $postglobid, 'createcat');
    $savetag = $this->savePostByTag($tags, $postglobid, 'createtag');

    $savepost = new Post;
    $savepost->post_global_id = $postglobid;
    $savepost->post_title = $postTitle;
    $savepost->post_desc = $postDesc;
    $savepost->post_img = $postimgnm;
    $savepost->post_img_gal = json_encode($galImg);
    $savepost->post_url = $posturl;

    $savepost->save();

    return redirect('/seo/viewallpost')->with('postSuccess', 'Blog has been Posted Successfully');
}
2 Answers

You can use cviebrock/eloquent-sluggable library to achieve this

https://github.com/cviebrock/eloquent-sluggable

Slugs tend to be unique as well. So if you write another post with the same title, you'd want to distinguish between them somehow, typically with an incremental counter added to the end of the slug:

http://example.com/post/my-dinner-with-andre-francois

http://example.com/post/my-dinner-with-andre-francois-1

http://example.com/post/my-dinner-with-andre-francois-2

$post = Post::create([
    'title' => 'My Awesome Blog Post',
]);
// $post->slug is "my-awesome-blog-post"

$newPost = $post->replicate();
// $newPost->slug is "my-awesome-blog-post-1"

To achieve this just update model

Your models should use the Sluggable trait, which has an abstract method sluggable() that you need to define.

use Cviebrock\EloquentSluggable\Sluggable;

class Post extends Model
{
    use Sluggable;

    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

Try to append the id of the record in the new slug. I believe the record id is unique.

Related