创建标题 slug + 随机字符串 Laravel 7

Create title slug + random string Laravel 7

我想创建一个 slug,它结合了标题 + Laravel 中的随机字符串。

我试过了,但是没有,在第二种情况下,它只是将字符串与标题结合起来。

Str::slug(request('title'), '-', Str::random());

Str::slug(request('title'), Str::random());

我想要这样的东西:

this-is-an-example-title-Jfij4jio4523q234

仔细检查 https://laravel.com/docs/7.x/helpers#method-str-slug (and deeper at https://github.com/laravel/framework/blob/7.x/src/Illuminate/Support/Str.php#L552)

中的方法签名

明确地说,该方法的第二个参数是用于替换空格的字符,第三个参数是指生成 slug 时要使用的语言环境。这意味着您需要在将字符串传递给方法之前将其完全组合。

假设你想让你的 slug 加入 - 那么你想要的就是这样的东西:

$value = request('title') . ' ' . Str::random();
$slug = Str::slug($value); // optionally Str::slug($value, '-'); to explicitly define the join

Str::Slug() 定义为:

slug(string $title, string $separator = '-', string $language = 'en')

第三个参数可以看作是language,默认值是:en.

Str::slug($request->input('title').Str::random(40), '-');

希望对您有所帮助。