mysql - 如何在 Laravel 5.8 中输入相同标题时创建独特的 slug

标签 mysql laravel laravel-5.8

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

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

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

这是我的 Controller 代码:

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

最佳答案

您可以使用cviebrock/eloquent-sluggable库来实现此目的

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

蛞蝓也往往是独一无二的。因此,如果您写另一篇具有相同标题的文章,您会希望以某种方式区分它们,通常会在标题末尾添加一个增量计数器:

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 特征,它具有您需要定义的抽象方法 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'
            ]
        ];
    }
}

关于mysql - 如何在 Laravel 5.8 中输入相同标题时创建独特的 slug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67954849/

相关文章:

php - Laravel 模型数据在 Controller 中不可用

php - 在 var/www/html/admin/vendor/nesbot/carbon/src/Carbon/Traits/Units.php 中遇到格式不正确的数值

php - 如何在 Eloquent ORM Laravel 5.8中保留 "updated_at"的同时删除 "created_at"

php - 使用类方法提取数据库信息

java - 如何检查Spring Boot使用的是什么数据库池?

android - 使用 davibennun/laravel-push-notification 包的 Laravel 推送通知

php - laravel 验证语法错误

php - 如何安装 Laravel 5.8

mysql - 使用 MySQL 查询导入 CSV 文件(指定列)

android - 将android应用程序连接到谷歌云