我想在Laravel中创建一个结合标题+随机字符串的slug.
我试过了,但是什么都没有,在第二种情况下,它什么都没有做,只是把字符串和标题结合起来.
Str::slug(request('title'), '-', Str::random());
或者
Str::slug(request('title'), Str::random());
我想要这样的东西。
This-is-an-example-title-Jfij4jio4523q234。
Str::Slug()
已经被定义为:
slug(string $title, string $separator = '-', string $language = 'en')
第三个参数可以看作是 language
用默认值。en
.
Str::slug($request->input('title').Str::random(40), '-');
希望对你有所帮助。
仔细检查方法签名,从 https:/laravel.comdocs7.xhelpers#method-str-slug。 (和更深层次的 https:/github.comlaravelframeworkblob7.xsrcIlluminateSupportStr.php#L552。)
明确地说,该方法的第二个参数是用来替换空格的字符,第三个参数是指生成slug时要使用的locale。这意味着你需要在将你的字符串传递给该方法之前将其完全组成。
假设你想让你的slug由一个叫 -
那么这样的东西是你想要的。
$value = request('title') . ' ' . Str::random();
$slug = Str::slug($value); // optionally Str::slug($value, '-'); to explicitly define the join