首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Laravel 5.8中输入相同标题时创建唯一的slug

如何在Laravel 5.8中输入相同标题时创建唯一的slug
EN

Stack Overflow用户
提问于 2021-06-13 12:26:40
回答 2查看 92关注 0票数 2

我想为同一标题创建唯一的弹头。

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

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

下面是我的控制器代码:

代码语言:javascript
复制
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');
}
EN

回答 2

Stack Overflow用户

发布于 2021-06-13 13:02:21

您可以使用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

代码语言:javascript
复制
$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()

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2021-06-13 12:30:59

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67954849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档