【问题标题】:How to create unique slug while entering same title in Laravel 5.8如何在 Laravel 5.8 中输入相同标题时创建独特的 slug
【发布时间】:2021-06-13 04:26:39
【问题描述】:

我想为同一个标题创建独特的 slug。

示例 - www.xcv.com/ght-ghy/

示例 1 - www.xcv.com/ght-ghy-1/

这是我的控制器代码:

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');
}

【问题讨论】:

    标签: mysql laravel laravel-5.8


    【解决方案1】:

    你可以使用cviebrock/eloquent-sluggable库来实现这个

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

    蛞蝓也往往是独一无二的。因此,如果您写另一篇具有相同标题的帖子,您会想以某种方式区分它们,通常在 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"
    

    要实现这一点,只需更新模型

    您的模型应该使用Sluggable trait,它有一个您需要定义的抽象方法sluggable()

    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'
                ]
            ];
        }
    }
    

    【讨论】:

      【解决方案2】:

      尝试在新的 slug 中附加记录的 id。我相信记录 ID 是唯一的。

      【讨论】:

      • 你能告诉我怎么做吗?添加我的代码....请
      猜你喜欢
      • 2017-06-01
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 2020-05-24
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多