deMemory 1年前

修改理由:

校对内容

相关信息:


此投稿由 神的孩子丶都在跳舞 1年前 合并。

标题修改:

+ 辅助函数

内容修改:

红色背景 为原始内容

绿色背景 为新增或者修改的内容

OldNewDifferences
88 - [Lottery](#lottery)
99
1010<a name="introduction"></a>
11   
1211## 简介
1312
1413Laravel 包含各种各样的全局 PHP 「辅助」函数,框架本身也大量的使用了这些功能函数;如果你觉的方便,你可以在你的应用中任意使用这些函数。
1514
1615<a name="available-methods"></a>
17   
1816## 可用方法
1917
2018<style>
2119   .collection-method-list > p {
2220       columns: 10.8em 3; -moz-columns: 10.8em 3; -webkit-columns: 10.8em 3;
2321   }
24   
2522
2623   .collection-method-list a {
2724       display: block;
 
3330</style>
3431
3532<a name="arrays-and-objects-method-list"></a>
36   
3733### 数组 & 对象
3834
3935<div class="collection-method-list" markdown="1">
40   
4136
4237[Arr::accessible](#method-array-accessible)
4338[Arr::add](#method-array-add)
 
8176[data_set](#method-data-set)
8277[head](#method-head)
8378[last](#method-last)
 79  
8480</div>
8581
8682<a name="paths-method-list"></a>
87   
8883### 路径
8984
9085<div class="collection-method-list" markdown="1">
91   
9286
9387[app_path](#method-app-path)
9488[base_path](#method-base-path)
 
10296
10397</div>
10498
105   
106   
10799<a name="strings-method-list"></a>
108   
109100### 字符串
110101
111102<div class="collection-method-list" markdown="1">
112   
113103
114104[\__](#method-__)
115105[class_basename](#method-class-basename)
 
183173</div>
184174
185175<a name="fluent-strings-method-list"></a>
186   
187176### 字符流处理
188177
189178<div class="collection-method-list" markdown="1">
190   
191179
192180[after](#method-fluent-str-after)
193181[afterLast](#method-fluent-str-after-last)
 
280268
281269</div>
282270
283   
284   
285271<a name="urls-method-list"></a>
286   
287272### URLs
288273
289274<div class="collection-method-list" markdown="1">
290   
291275
292276[action](#method-action)
293277[asset](#method-asset)
 
300284</div>
301285
302286<a name="miscellaneous-method-list"></a>
303   
304287### 杂项
305288
306289<div class="collection-method-list" markdown="1">
307   
308290
309291[abort](#method-abort)
310292[abort_if](#method-abort-if)
 
362344</div>
363345
364346<a name="method-listing"></a>
365   
366347## 方法列表
367348
368349<style>
 
370351       font-size: 14px;
371352   }
372353
373   
374354   .collection-method:not(.first-collection-method) {
375355       margin-top: 50px;
376356   }
 
378358</style>
379359
380360<a name="arrays"></a>
381   
382361## 数组 & 对象
383362
384363<a name="method-array-accessible"></a>
385   
386364#### `Arr::accessible()` {.collection-method .first-collection-method}
387365
388366`Arr::accessible` 方法检查给定的值是否可被数组式访问:
 
407385   // false
408386
409387<a name="method-array-add"></a>
410   
411388#### `Arr::add()` {.collection-method}
412389
413390如果给定的键名在数组中不存在键值或该键值设置为 `null` ,那么 `Arr::add` 方法将会把给定的键值对添加到数组中:
 
422399   
423400   // ['name' => 'Desk', 'price' => 100]
424401
425   
426   
427   
428402<a name="method-array-collapse"></a>
429   
430403#### `Arr::collapse()` {.collection-method}
431404
432405`Arr::collapse` 方法将多个数组合并为一个数组:
 
438411   // [1, 2, 3, 4, 5, 6, 7, 8, 9]
439412
440413<a name="method-array-crossjoin"></a>
441   
442414#### `Arr::crossJoin()` {.collection-method}
443415
444416`Arr::crossJoin` 方法交叉连接给定的数组,返回具有所有可能排列的笛卡尔乘积:
 
472444   */
473445
474446<a name="method-array-divide"></a>
475   
476447#### `Arr::divide()` {.collection-method}
477448
478449`Arr::divide` 方法返回一个二维数组,一个值包含原数组的键,另一个值包含原数组的值:
 
486457   // $values: ['Desk']
487458
488459<a name="method-array-dot"></a>
489   
490460#### `Arr::dot()` {.collection-method}
491461
492462`Arr::dot` 方法将多维数组中所有的键平铺到一维数组中,新数组使用「.」符号表示层级包含关系:
 
500470   // ['products.desk.price' => 100]
501471
502472<a name="method-array-except"></a>
503   
504473#### `Arr::except()` {.collection-method}
505474
506475`Arr::except` 方法从数组中删除指定的键值对:
 
514483   // ['name' => 'Desk']
515484
516485<a name="method-array-exists"></a>
517   
518486#### `Arr::exists()` {.collection-method}
519487
520488`Arr::exists` 方法检查给定的键是否存在提供的数组中:
 
532500   // false
533501
534502<a name="method-array-first"></a>
535   
536   
537503#### `Arr::first()` {.collection-method}
538504
539505`Arr::first` 方法返回数组中满足指定条件的第一个元素:
 
569535   // ['Joe', 'PHP', 'Ruby']
570536
571537<a name="method-array-forget"></a>
572   
573538#### `Arr::forget()` {.collection-method}
574539
575540`Arr::forget` 方法使用「.」符号从深度嵌套的数组中删除给定的键值对:
 
583548   // ['products' => []]
584549
585550<a name="method-array-get"></a>
586   
587551#### `Arr::get()` {.collection-method}
588552
589553`Arr::get` 方法使用「.」符号从深度嵌套的数组中根据指定键检索值:
 
605569   // 0
606570
607571<a name="method-array-has"></a>
608   
609572#### `Arr::has()` {.collection-method}
610573
611574`Arr::has` 方法使用「.」符号判断数组中是否存在指定的一个或多个键:
 
622585   
623586   // false
624587
625   
626   
627588<a name="method-array-hasany"></a>
628   
629589#### `Arr::hasAny()` {.collection-method}
630590
631591`Arr::hasAny` 方法使用「.」符号判断给定集合中的任一值是否存在于数组中:
 
647607   // false
648608
649609<a name="method-array-isassoc"></a>
650   
651610#### `Arr::isAssoc()` {.collection-method}
652611
653612如果给定数组是关联数组,则 `Arr::isAssoc` 方法返回 `true`,如果该数组没有以零开头的顺序数字键,则将其视为「关联」数组:
 
663622   // false
664623
665624<a name="method-array-islist"></a>
666   
667625#### `Arr::isList()` {.collection-method}
668626
669627如果给定数组的键是从零开始的连续整数,则 `Arr::isList` 方法返回 `true`:
 
679637   // false
680638
681639<a name="method-array-join"></a>
682   
683640#### `Arr::join()` {.collection-method}
684641
685642`Arr::join()`方法将给定数组的所有值通过给定字符串连接起来。使用此方法的第二个参数,您还可以为数组中的最后一个元素指定连接的字符串:
 
697654   // Tailwind, Alpine, Laravel and Livewire
698655
699656<a name="method-array-keyby"></a>
700   
701657#### `Arr::keyBy()` {.collection-method}
702658
703659`Arr::keyBy()`方法通过给定键名的值对该数组进行重组。如果数组中存在多个相同的值,则只有最后一个值会出现在新数组中:
 
718674       ]
719675   */
720676
721   
722   
723677<a name="method-array-last"></a>
724   
725678#### `Arr::last()` {.collection-method}
726679
727680`Arr::last` 方法返回数组中满足指定条件的最后一个元素:
 
743696   $last = Arr::last($array, $callback, $default);
744697
745698<a name="method-array-map"></a>
746   
747699#### `Arr::map()` {.collection-method}
748700
749701`Arr::map` 方法用来遍历数组,并将每个值和键传递给给定的回调。数组值由回调返回的值替换:
 
759711   // ['first' => 'James', 'last' => 'Kirk']
760712
761713<a name="method-array-only"></a>
762   
763714#### `Arr::only()` {.collection-method}
764715
765716`Arr::only` 方法仅返回给定数组中的指定键/值对:
 
773724   // ['name' => 'Desk', 'price' => 100]
774725
775726<a name="method-array-pluck"></a>
776   
777727#### `Arr::pluck()` {.collection-method}
778728
779729`Arr::pluck` 方法从数组中检索给定键的所有值:
 
799749
800750<a name="method-array-prepend"></a>
801751
802   
803752<a name="method-array-last"></a>
804   
805753#### `Arr::last()` {.collection-method}
806754
807755`Arr::last` 方法返回数组中满足指定条件的最后一个元素:
 
823771   $last = Arr::last($array, $callback, $default);
824772
825773<a name="method-array-map"></a>
826   
827774#### `Arr::map()` {.collection-method}
828775
829776`Arr::map` 方法遍历数组并将每个键和值传递至给定的回调方法。数组的值将替换为该回调方法返回的值:
 
839786   // ['first' => 'James', 'last' => 'Kirk']
840787
841788<a name="method-array-only"></a>
842   
843789#### `Arr::only()` {.collection-method}
844790
845791`Arr::only` 方法只返回给定数组中指定的键值对:
 
853799   // ['name' => 'Desk', 'price' => 100]
854800
855801<a name="method-array-pluck"></a>
856   
857802#### `Arr::pluck()` {.collection-method}
858803
859804`Arr::pluck` 方法从数组中检索给定键的所有值:
 
878823   // [1 => 'Taylor', 2 => 'Abigail']
879824
880825<a name="method-array-prepend"></a>
881   
882   
883826#### `Arr::prepend()` {.collection-method}
884827
885828`Arr::prepend` 方法将一个值插入到数组的开始位置:
 
903846   // ['name' => 'Desk', 'price' => 100]
904847
905848<a name="method-array-prependkeyswith"></a>
906   
907849#### `Arr::prependKeysWith()` {.collection-method}
908850
909851`Arr::prependKeysWith` 方法为关联数组中的所有键添加给定前缀:
 
925867   */
926868
927869<a name="method-array-pull"></a>
928   
929870#### `Arr::pull()` {.collection-method}
930871
931872`Arr::pull` 方法从数组中返回指定键的值并删除此键值对:
 
947888   $value = Arr::pull($array, $key, $default);
948889
949890<a name="method-array-query"></a>
950   
951891#### `Arr::query()` {.collection-method}
952892
953893`Arr::query` 方法将数组转换为查询字符串:
 
967907   // name=Taylor&order[column]=created_at&order[direction]=desc
968908
969909<a name="method-array-random"></a>
970   
971910#### `Arr::random()` {.collection-method}
972911
973912`Arr::random` 方法从数组中随机返回一个值:
 
988927   
989928   // [2, 5] - (retrieved randomly)
990929
991   
992   
993930<a name="method-array-set"></a>
994   
995931#### `Arr::set()` {.collection-method}
996932
997933`Arr::set` 方法使用「.」符号在多维数组中设置指定键的值:
 
1005941   // ['products' => ['desk' => ['price' => 200]]]
1006942
1007943<a name="method-array-shuffle"></a>
1008   
1009944#### `Arr::shuffle()` {.collection-method}
1010945
1011946`Arr::shuffle` 方法将数组中值进行随机排序:
 
1017952   // [3, 2, 5, 1, 4] - (generated randomly)
1018953
1019954<a name="method-array-sort"></a>
1020   
1021955#### `Arr::sort()` {.collection-method}
1022956
1023957`Arr::sort` 方法根据给定数组的值进行升序排序:
 
1053987   */
1054988
1055989<a name="method-array-sort-desc"></a>
1056   
1057990#### `Arr::sortDesc()` {.collection-method}
1058991
1059992`Arr::sortDesc` 方法根据给定数组的值进行降序排序:
 
10891022   */
10901023
10911024<a name="method-array-sort-recursive"></a>
1092   
10931025#### `Arr::sortRecursive()` {.collection-method}
10941026
10951027`Arr::sortRecursive` 方法对给定数组进行递归排序,使用 `sort` 方法对数字索引子数组进行按值升序排序,使用 `ksort` 方法对关联子数组进行按键升序排序:
 
11151047
11161048
11171049<a name="method-array-to-css-classes"></a>
1118   
11191050#### `Arr::toCssClasses()` {.collection-method}
11201051
11211052`Arr::toCssClasses` 方法根据给定的条件编译并返回 CSS 类字符串。该方法接受一个类数组,其中数组键包含你希望添加的一个或多个 CSS Class,而值是一个布尔表达式。如果数组元素有一个数字键,它将始终包含在呈现的类列表中:
 
11331064       'p-4 bg-red'
11341065   */
11351066
1136 Laravel 基于该方法实现 [Blade组件里的条件合并类](/docs/laravel/10.x/blade#conditionally-merge-classes) 以及 `@class` [Blade指令](/docs/laravel/10.x/blade#conditional-classes):
 1067Laravel 基于该方法实现 [Blade 组件里的条件合并类](/docs/laravel/10.x/blademd#conditionally-merge-classes) 以及 `@class` [Blade 指令](/docs/laravel/10.x/blademd#conditional-classes):
11371068
11381069<a name="method-array-undot"></a>
1139   
11401070#### `Arr::undot()` {.collection-method}
11411071
11421072`Arr::undot` 方法使用「.」符号将一维数组扩展为多维数组:
 
11691099   // [1 => '200', 3 => '400']
11701100
11711101<a name="method-array-where-not-null"></a>
1172   
11731102#### `Arr::whereNotNull()` {.collection-method}
11741103
11751104`Arr::whereNotNull` 方法将从给定数组中删除所有 `null` 值:
 
11831112   // [0 => 0]
11841113
11851114<a name="method-array-wrap"></a>
1186   
11871115#### `Arr::wrap()` {.collection-method}
11881116
11891117`Arr::wrap` 方法可以将给定值转换为一个数组,如果给定的值已经是一个数组,它将原样返回:
 
11961124   
11971125   // ['Laravel']
11981126
1199   
1200   
12011127如果给定值是 `null` ,将返回一个空数组:
12021128
12031129   use Illuminate\Support\Arr;
 
12071133   // []
12081134
12091135<a name="method-data-fill"></a>
1210   
12111136#### `data_fill()` {#collection-method}
12121137
12131138`data_fill` 函数使用「.」符号给多维数组或对象设置缺少的值:
 
12431168   */
12441169
12451170<a name="method-data-get"></a>
1246   
12471171#### `data_get()` {#collection-method}
12481172
12491173`data_get` 函数使用 「.」 符号从多维数组或对象中根据指定键检索值
 
12721196   // ['Desk 1', 'Desk 2'];
12731197
12741198<a name="method-data-set"></a>
1275   
12761199#### `data_set()` {#collection-method}
12771200
12781201`data_set` 函数使用「.」符号从多维数组或对象中根据指定键设置值:
 
13031226       ]
13041227   */
13051228
1306   
1307   
13081229通常情况下,已存在的值将会被覆盖。如果只是希望设置一个目前不存在的值,你可以增加一个 `false` 作为函数的第四个参数:
13091230
13101231   $data = ['products' => ['desk' => ['price' => 100]]];
 
13141235   // ['products' => ['desk' => ['price' => 100]]]
13151236
13161237<a name="method-head"></a>
1317   
13181238#### `head()` {#collection-method}
13191239
13201240`head` 函数将返回数组中的第一个值:
 
13381258   // 300
13391259
13401260<a name="paths"></a>
1341   
13421261## 路径
13431262
13441263<a name="method-app-path"></a>
1345   
13461264#### `app_path()` {.collection-method}
13471265
13481266`app_path` 函数返回 `app` 目录的完整路径。你也可以使用 `app_path` 函数来生成应用目录下特定文件的完整路径:
 
13521270   $path = app_path('Http/Controllers/Controller.php');
13531271
13541272<a name="method-base-path"></a>
1355   
13561273#### `base_path()` {#collection-method}
13571274
13581275`base_path` 函数返回项目根目录的完整路径。你也可以使用 `base_path` 函数生成项目根目录下特定文件的完整路径:
 
13621279   $path = base_path('vendor/bin');
13631280
13641281<a name="method-config-path"></a>
1365   
13661282#### `config_path()` {#collection-method}
13671283
13681284`config_path` 函数返回项目配置目录 (config) 的完整路径。你也可以使用 `config_path` 函数来生成应用配置目录中的特定文件的完整路径:
 
13721288   $path = config_path('app.php');
13731289
13741290<a name="method-database-path"></a>
1375   
1376   
13771291#### `database_path()` {.collection-method}
13781292
13791293`database_path` 函数返回 `database` 目录的完整路径。你可以使用 `database_path` 函数来生成数据库目录下指定文件的完整路径:
 
13831297   $path = database_path('factories/UserFactory.php');
13841298
13851299<a name="method-lang-path"></a>
1386   
13871300#### `lang_path()` {.collection-method}
13881301
13891302The `lang_path` 函数返回 `lang` 目录的完整路径。你可以使用 `lang_path` 函数来生成自定义语言目录下指定文件的完整路径:
 
13961309> 默认情况下,Laravel 框架不包含 `lang` 目录。如果你想自定义 Laravel 的语言文件,可以通过 Artisan 命令 `lang:publish` 来发布它们。
13971310
13981311<a name="method-mix"></a>
1399   
14001312#### `mix()` {.collection-method}
14011313
14021314`mix` 函数返回 [编译前端资源(Mix)的路径](/docs/laravel/10.x/mix),便于加载 css,js 等静态文件:
 
14041316   $path = mix('css/app.css');
14051317
14061318<a name="method-public-path"></a>
1407   
14081319#### `public_path()` {.collection-method}
14091320
14101321`public_path` 函数返回 `public` 目录的完整路径。你可以使用 `public_path` 函数来生成`public` 目录下指定文件的完整路径:
 
14141325   $path = public_path('css/app.css');
14151326
14161327<a name="method-resource-path"></a>
1417   
14181328#### `resource_path()` {.collection-method}
14191329
14201330`resource_path` 函数返回 `resource` 目录的完整路径。你可以使用 `resource_path` 函数来生成位于资源路径中指定文件的完整路径:
 
14231333   
14241334   $path = resource_path('sass/app.scss');
14251335
1426   
1427   
14281336<a name="method-storage-path"></a>
1429   
14301337#### `storage_path()`
14311338
14321339`storage_path` 函数返回 `storage` 目录的完整路径。 你也可以用 `storage_path` 函数来生成位于资源路径中的特定文件路径
 
16041511   // true
16051512
16061513<a name="method-str-contains-all"></a>
1607   
16081514#### `Str::containsAll()`
16091515
16101516`Str::containsAll` 方法用于判断指定字符串是否包含指定数组中的所有值:
 
16161522   // true
16171523
16181524<a name="method-ends-with"></a>
1619   
16201525#### `Str::endsWith()`
16211526
16221527`Str::endsWith` 方法用于判断指定字符串是否以另一指定字符串结尾:
 
16401545   // false
16411546
16421547<a name="method-excerpt"></a>
1643   
16441548#### `Str::excerpt()`
16451549
16461550`Str::excerpt` 方法提取字符串中给定短语匹配到的第一个片段:
 
16541558   // '...is my na...'
16551559
16561560`radius` 选项默认为 `100`,允许你定义应出现在截断字符串前后的字符数。
1657   
1658   
16591561
16601562此外,你可以使用`omission`选项来定义将附加到截断字符串的字符串:
16611563
 
16851587   // this/string/
16861588
16871589<a name="method-str-headline"></a>
1688   
16891590#### `Str::headline()` {.collection-method}
16901591
16911592`Str::headline`方法会将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串,同时保证每个单词的首字母大写:
 
17011602   // 邮件通知发送
17021603
17031604<a name="method-str-inline-markdown"></a>
1704   
17051605#### `Str::inlineMarkdown()` {.collection-method}
17061606
17071607`Str::inlineMarkdown`方法使用[通用标记](https://commonmark.thephpleague.com/)将 GitHub 风味 Markdown 转换为内联 HTML。然而,与`markdown`方法不同的是,它不会将所有生成的 HTML 都包装在块级元素中:
 
17131613   // <strong>Laravel</strong>
17141614
17151615<a name="method-str-is"></a>
1716   
17171616#### `Str::is()` {.collection-method}
17181617
17191618`Str::is`方法用来判断字符串是否与指定模式匹配。星号`*`可用于表示通配符:
 
17291628   // false
17301629
17311630<a name="method-str-is-ascii"></a>
1732   
17331631#### `Str::isAscii()` {.collection-method}
17341632
17351633`Str::isAscii`方法用于判断字符串是否是 7 位 ASCII:
 
17441642   
17451643   // false
17461644
1747   
1748   
17491645<a name="method-str-is-json"></a>
1750   
17511646#### `Str::isJson()` {.collection-method}
17521647
17531648`Str::isJson`方法确定给定的字符串是否是有效的 JSON:
 
17671662   // false
17681663
17691664<a name="method-str-is-ulid"></a>
1770   
17711665#### `Str::isUlid()` {.collection-method}
17721666
17731667`Str::isUlid`方法用于判断指定字符串是否是有效的 ULID:
 
17831677   // false
17841678
17851679<a name="method-str-is-uuid"></a>
1786   
17871680#### `Str::isUuid()` {.collection-method}
17881681
17891682`Str::isUuid`方法用于判断指定字符串是否是有效的 UUID:
 
17991692   // false
18001693
18011694<a name="method-kebab-case"></a>
1802   
18031695#### `Str::kebab()` {.collection-method}
18041696
18051697`Str::kebab`方法将字符串转换为`烤串式( kebab-case )`表示方法:
 
18111703   // foo-bar
18121704
18131705<a name="method-str-lcfirst"></a>
1814   
18151706#### `Str::lcfirst()` {.collection-method}
18161707
18171708`Str::lcfirst`方法返回第一个小写字符的给定字符串:
 
18231714   // foo Bar
18241715
18251716<a name="method-str-length"></a>
1826   
18271717#### `Str::length()` {.collection-method}
18281718
18291719`Str::length`方法返回指定字符串的长度:
 
18351725   // 7
18361726
18371727<a name="method-str-limit"></a>
1838   
18391728#### `Str::limit()` {.collection-method}
18401729
18411730`Str::limit`方法将字符串以指定长度进行截断:
 
18461735   
18471736   // 敏捷的棕色狐狸...
18481737
1849   
1850   
18511738你也可通过第三个参数来改变追加到末尾的字符串:
18521739
18531740   use Illuminate\Support\Str;
 
18571744   // 敏捷的棕色狐狸 (...)
18581745
18591746<a name="method-str-lower"></a>
1860   
18611747#### `Str::lower()` {.collection-method}
18621748
18631749`Str::lower`方法用于将字符串转换为小写:
 
18691755   // laravel
18701756
18711757<a name="method-str-markdown"></a>
1872   
18731758#### `Str::markdown()` {.collection-method}
18741759
1875 `Str::markdown`方法将 GitHub 风格的 Markdown 转换为 HTML 使用[通用标记](https://commonmark.thephpleague.com/):
 1760`Str::markdown`方法将 GitHub 风格的 Markdown 转换为 HTML 使用 [通用标记](https://commonmark.thephpleague.com/):
18761761
18771762   use Illuminate\Support\Str;
18781763   
 
18871772   // <h1>Taylor Otwell</h1>
18881773
18891774<a name="method-str-mask"></a>
1890   
18911775#### `Str::mask()` {.collection-method}
18921776
18931777`Str::mask`方法会使用重复的字符掩盖字符串的一部分,并可用于混淆字符串段,例如电子邮件地址和电话号码:
 
19051789   // tay***@example.com
19061790
19071791<a name="method-str-ordered-uuid"></a>
1908   
19091792#### `Str::orderedUuid()` {.collection-method}
19101793
19111794`Str::orderedUuid`方法用于生成一个「时间戳优先」的 UUID ,它可作为数据库索引列的有效值。使用此方法生成的每个 UUID 将排在之前使用该方法生成的 UUID 后面:
 
19141797   
19151798   return (string) Str::orderedUuid();
19161799
1917   
1918   
19191800<a name="method-str-padboth"></a>
1920   
19211801#### `Str::padBoth()` {.collection-method}
19221802
19231803`Str::padBoth`方法包装了 PHP 的`str_pad 方法`,在指定字符串的两侧填充上另一字符串:
 
19331813   // ' James  '
19341814
19351815<a name="method-str-padleft"></a>
1936   
19371816#### `Str::padLeft()` {.collection-method}
19381817
19391818`Str::padLeft`方法包装了 PHP 的`str_pad`方法,在指定字符串的左侧填充上另一字符串:
 
19491828   // '    James'
19501829
19511830<a name="method-str-padright"></a>
1952   
19531831#### `Str::padRight()` {.collection-method}
19541832
19551833`Str::padRight`方法包装了 PHP 的`str_pad`方法,在指定字符串的右侧填充上另一字符串:
 
19651843   // 'James    '
19661844
19671845<a name="method-str-password"></a>
1968   
19691846#### `Str::password()` {.collection-method}
19701847
19711848`Str::password`方法可用于生成给定长度的安全随机密码。密码由字母、数字、符号和空格组成。默认情况下,密码长度为32位:
 
19811858   // 'qwuar>#V|i]N'
19821859
19831860<a name="method-str-plural"></a>
1984   
19851861#### `Str::plural()` {.collection-method}
19861862
1987 `Str::plural`方法将单数形式的字符串转换为复数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization#pluralization-language):
 1863`Str::plural`方法将单数形式的字符串转换为复数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localizationmd#pluralization-language):
19881864
19891865   use Illuminate\Support\Str;
19901866   
 
19961872   
19971873   // children
19981874
1999   
2000   
20011875你可以提供一个整数作为方法的第二个参数来检索字符串的单数或复数形式:
20021876
20031877   use Illuminate\Support\Str;
 
20111885   // child
20121886
20131887<a name="method-str-plural-studly"></a>
2014   
20151888#### `Str::pluralStudly()` {.collection-method}
20161889
20171890`Str::pluralStudly`方法将以驼峰格式的单数字符串转化为其复数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization#pluralization-language):
 
20391912   // VerifiedHuman
20401913
20411914<a name="method-str-random"></a>
2042   
20431915#### `Str::random()` {.collection-method}
20441916
20451917`Str::random` 方法用于生成指定长度的随机字符串。这个方法使用了PHP的 `random_bytes` 函数:
 
20491921   $random = Str::random(40);
20501922
20511923<a name="method-str-remove"></a>
2052   
20531924#### `Str::remove()` {.collection-method}
20541925
20551926`Str::remove` 方法从字符串中删除给定值或给定数组内的所有值:
 
20651936你还可以将`false`作为第三个参数传递给`remove`方法以在删除字符串时忽略大小写。
20661937
20671938<a name="method-str-replace"></a>
2068   
20691939#### `Str::replace()` {.collection-method}
20701940
20711941`Str::replace` 方法用于替换字符串中的给定字符串:
20721942
20731943   use Illuminate\Support\Str;
20741944   
2075    $string = 'Laravel 8.x';
2076    
2077    $replaced = Str::replace('8.x', '9.x', $string);
2078    
2079    // Laravel 9.x
2080 
2081 
 1945   $string = 'Laravel 10.x';
 1946   
 1947   $replaced = Str::replace('9.x', '10.x', $string);
 1948   
 1949   // Laravel 10.x
20821950
20831951<a name="method-str-replace-array"></a>
2084   
20851952#### `Str::replaceArray()` {.collection-method}
20861953
20871954`Str::replaceArray` 方法使用数组有序的替换字符串中的特定字符:
 
20951962   // 该活动将在 8:30 至 9:00 举行
20961963
20971964<a name="method-str-replace-first"></a>
2098   
20991965#### `Str::replaceFirst()` {.collection-method}
21001966
21011967`Str::replaceFirst` 方法替换字符串中给定值的第一个匹配项:
 
21071973   // a quick brown fox jumps over the lazy dog
21081974
21091975<a name="method-str-replace-last"></a>
2110   
21111976#### `Str::replaceLast()` {.collection-method}
21121977
21131978`Str::replaceLast` 方法替换字符串中最后一次出现的给定值:
 
21181983   
21191984   // the quick brown fox jumps over a lazy dog
21201985
2121   
21221986<a name="method-str-reverse"></a>
2123   
21241987#### `Str::reverse()` {.collection-method}
21251988
21261989`Str::reverse` 方法用于反转给定的字符串:
 
21321995   // dlroW olleH
21331996
21341997<a name="method-str-singular"></a>
2135   
21361998#### `Str::singular()` {.collection-method}
21371999
2138 `Str::singular` 方法将字符串转换为单数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization#pluralization-language):
 2000`Str::singular` 方法将字符串转换为单数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localizationmd#pluralization-language):
21392001
21402002   use Illuminate\Support\Str;
21412003   
 
21482010   // child
21492011
21502012<a name="method-str-slug"></a>
2151   
21522013#### `Str::slug()` {.collection-method}
21532014
21542015`Str::slug` 方法将给定的字符串生成一个 URL 友好的「slug」:
21552016
21562017   use Illuminate\Support\Str;
21572018   
2158    $slug = Str::slug('Laravel 5 Framework', '-');
2159    
2160    // laravel-5-framework
2161 
2162 
 2019   $slug = Str::slug('Laravel 10 Framework', '-');
 2020   
 2021   // laravel-10-framework
21632022
21642023<a name="method-snake-case"></a>
2165   
21662024#### `Str::snake()` {.collection-method}
21672025
21682026`Str::snake` 方法是将驼峰的函数名或者字符串转换成 `_` 命名的函数或者字符串,例如 `snakeCase` 转换成 `snake_case`:
 
21782036   // foo-bar
21792037
21802038<a name="method-str-squish"></a>
2181   
21822039#### `Str::squish()` {.collection-method}
21832040
21842041`Str::squish`方法删除字符串中所有多余的空白,包括单词之间多余的空白:
 
21902047   // laravel framework
21912048
21922049<a name="method-str-start"></a>
2193   
21942050#### `Str::start()` {.collection-method}
21952051
21962052`Str::start`方法是将给定的值添加到字符串的开始位置,例如:
 
22062062   // /this/string
22072063
22082064<a name="method-starts-with"></a>
2209   
22102065#### `Str::startsWith()` {.collection-method}
22112066
22122067`Str::startsWith`方法用来判断给定的字符串是否为给定值的开头:
 
22242079   // true
22252080
22262081<a name="method-studly-case"></a>
2227   
22282082#### `Str::studly()` {.collection-method}
22292083
22302084`Str::studly`方法将给定的字符串转换为`驼峰命名`的字符串:
 
22362090   // FooBar
22372091
22382092<a name="method-str-substr"></a>
2239   
22402093#### `Str::substr()` {.collection-method}
22412094
22422095`Str::substr`方法返回由 start 和 length 参数指定的字符串部分:
 
22472100   
22482101   // Laravel
22492102
2250   
2251   
22522103<a name="method-str-substrcount"></a>
2253   
22542104#### `Str::substrCount()` {.collection-method}
22552105
22562106`Str::substrCount` 方法返回给定字符串中给定值的出现次数:
 
22622112   // 2
22632113
22642114<a name="method-str-substrreplace"></a>
2265   
22662115#### `Str::substrReplace()` {.collection-method}
22672116
22682117`Str::substrReplace` 方法替换字符串一部分中的文本,从第三个参数指定的位置开始,替换第四个参数指定的字符数。 当「0」传递给方法的第四个参数将在指定位置插入字符串,而不是替换字符串中的任何现有字符:
 
22762125   // 13:00
22772126
22782127<a name="method-str-swap"></a>
2279   
22802128#### `Str::swap()` {.collection-method}
22812129
22822130`Str::swap` 方法使用 PHP 的 `strtr` 函数替换给定字符串中的多个值:
 
22882136       'great' => 'fantastic',
22892137   ], 'Tacos are great!');
22902138   
2291    // Burritos are fantastic!
 2139   // Burritos are fantastic
22922140
22932141<a name="method-title-case"></a>
2294   
22952142#### `Str::title()` {.collection-method}
22962143
22972144`Str::title` 方法将给定的字符串转换为 `Title Case`:
 
23032150   // A Nice Title Uses The Correct Case
23042151
23052152<a name="method-str-to-html-string"></a>
2306   
23072153#### `Str::toHtmlString()` {.collection-method}
23082154
23092155`Str::toHtmlString` 方法将字符串实例转换为 `Illuminate\Support\HtmlString` 的实例,它可以显示在 Blade 模板中:
 
23132159   $htmlString = Str::of('Nuno Maduro')->toHtmlString();
23142160
23152161<a name="method-str-ucfirst"></a>
2316   
23172162#### `Str::ucfirst()` {.collection-method}
23182163
23192164`Str::ucfirst` 方法返回第一个字符大写的给定字符串:
 
23242169   
23252170   // Foo bar
23262171
2327   
2328   
23292172<a name="method-str-ucsplit"></a>
2330   
23312173#### `Str::ucsplit()` {.collection-method}
23322174
23332175`Str::ucsplit` 方法将给定的字符串按大写字符拆分为数组:
 
23392181   // [0 => 'Foo', 1 => 'Bar']
23402182
23412183<a name="method-str-upper"></a>
2342   
23432184#### `Str::upper()` {.collection-method}
23442185
23452186`Str::upper` 方法将给定的字符串转换为大写:
 
23512192   // LARAVEL
23522193
23532194<a name="method-str-ulid"></a>
2354   
23552195#### `Str::ulid()` {.collection-method}
23562196
23572197`Str::ulid` 方法生成一个 ULID:
 
23632203   // 01gd6r360bp37zj17nxb55yv40
23642204
23652205<a name="method-str-uuid"></a>
2366   
23672206#### `Str::uuid()` {.collection-method}
23682207
23692208`Str::uuid` 方法生成一个 UUID(版本 4):
 
23732212   return (string) Str::uuid();
23742213
23752214<a name="method-str-word-count"></a>
2376   
23772215#### `Str::wordCount()` {.collection-method}
23782216
23792217`Str::wordCount` 方法返回字符串包含的单词数
 
23852223```
23862224
23872225<a name="method-str-words"></a>
2388   
23892226#### `Str::words()` {.collection-method}
23902227
23912228`Str::words` 方法限制字符串中的单词数。 可以通过其第三个参数将附加字符串传递给此方法,以指定应将这个字符串附加到截断后的字符串末尾:
 
23972234   // Perfectly balanced, as >>>
23982235
23992236<a name="method-str"></a>
2400   
24012237#### `str()` {.collection-method}
24022238
24032239`str` 函数返回给定字符串的新 `Illuminate\Support\Stringable` 实例。 此函数等效于 `Str::of` 方法:
 
24122248   
24132249   // 'foo_bar'
24142250
2415   
2416   
24172251<a name="method-trans"></a>
2418   
24192252#### `trans()` {.collection-method}
24202253
2421 `trans` 函数使用你的 [语言文件](/docs/laravel/10.x/localization) 翻译给定的翻译键:
 2254`trans` 函数使用你的 [语言文件](/docs/laravel/10.x/localizationmd) 翻译给定的翻译键:
24222255
24232256```
24242257    echo trans('messages.welcome');
 
24392272如果指定的翻译键不存在,`trans_choice` 函数将返回给定的键。 因此,使用上面的示例,如果翻译键不存在,`trans_choice` 函数将返回 `messages.notifications`。
24402273
24412274<a name="fluent-strings"></a>
2442   
24432275## 字符流处理
24442276
24452277Fluent strings 提供了一个更流畅的、面向对象的接口来处理字符串值,与传统的字符串操作相比,允许你使用更易读的语法将多个字符串操作链接在一起。
24462278
24472279<a name="method-fluent-str-after"></a>
2448   
24492280#### `after` {.collection-method}
24502281
24512282`after` 方法返回字符串中给定值之后的所有内容。 如果字符串中不存在该值,则将返回整个字符串:
 
24572288   // ' my name'
24582289
24592290<a name="method-fluent-str-after-last"></a>
2460   
24612291#### `afterLast` {.collection-method}
24622292
24632293`afterLast` 方法返回字符串中最后一次出现给定值之后的所有内容。 如果字符串中不存在该值,则将返回整个字符串
 
24682298   
24692299   // 'Controller'
24702300
2471   
2472   
24732301<a name="method-fluent-str-append"></a>
2474   
24752302#### `append` {.collection-method}
24762303
24772304`append` 方法将给定的值附加到字符串:
 
24832310   // 'Taylor Otwell'
24842311
24852312<a name="method-fluent-str-ascii"></a>
2486   
24872313#### `ascii` {.collection-method}
24882314
24892315`ascii` 方法将尝试将字符串音译为 ASCII 值:
 
24952321   // 'u'
24962322
24972323<a name="method-fluent-str-basename"></a>
2498   
24992324#### `basename` {.collection-method}
25002325
25012326`basename` 方法将返回给定字符串的结尾名称部分:
 
25152340   // 'baz'
25162341
25172342<a name="method-fluent-str-before"></a>
2518   
25192343#### `before` {.collection-method}
25202344
25212345`before` 方法返回字符串中给定值之前的所有内容:
 
25272351   // 'This is '
25282352
25292353<a name="method-fluent-str-before-last"></a>
2530   
25312354#### `beforeLast` {.collection-method}
25322355
25332356`beforeLast` 方法返回字符串中最后一次出现给定值之前的所有内容:
 
25392362   // 'This '
25402363
25412364<a name="method-fluent-str-between"></a>
2542   
25432365#### `between` {.collection-method}
25442366
25452367`between` 方法返回两个值之间的字符串部分:
 
25512373   // ' is my '
25522374
25532375<a name="method-fluent-str-between-first"></a>
2554   
25552376#### `betweenFirst` {.collection-method}
25562377
25572378`betweenFirst` 方法返回两个值之间字符串的最小可能部分:
 
25622383   
25632384   // 'a'
25642385
2565   
2566   
25672386<a name="method-fluent-str-camel"></a>
2568   
25692387#### `camel` {.collection-method}
25702388
25712389`camel` 方法将给定的字符串转换为 `camelCase`:
 
25772395   // fooBar
25782396
25792397<a name="method-fluent-str-class-basename"></a>
2580   
25812398#### `classBasename` {.collection-method}
25822399
25832400`classBasename` 方法返回给定类的类名,删除了类的命名空间:
 
25892406   // Baz
25902407
25912408<a name="method-fluent-str-contains"></a>
2592   
25932409#### `contains` {.collection-method}
25942410
25952411`contains` 方法确定给定的字符串是否包含给定的值。 此方法区分大小写:
 
26092425   // true
26102426
26112427<a name="method-fluent-str-contains-all"></a>
2612   
26132428#### `containsAll` {.collection-method}
26142429
26152430`containsAll` 方法确定给定字符串是否包含给定数组中的所有值:
 
26212436   // true
26222437
26232438<a name="method-fluent-str-dirname"></a>
2624   
26252439#### `dirname` {.collection-method}
26262440
26272441`dirname` 方法返回给定字符串的父目录部分:
 
26412455   // '/foo'
26422456
26432457<a name="method-fluent-str-excerpt"></a>
2644   
26452458#### `excerpt` {.collection-method}
26462459
26472460`excerpt` 方法从字符串中提取与该字符串中短语的第一个实例匹配的摘录:
 
26532466   ]);
26542467   
26552468   // '...is my na...'
2656   
2657   
26582469
26592470`radius` 选项默认为 `100`,允许你定义应出现在截断字符串每一侧的字符数。
26602471
 
26702481   // '(...) my name'
26712482
26722483<a name="method-fluent-str-ends-with"></a>
2673   
26742484#### `endsWith` {.collection-method}
26752485
26762486`endsWith` 方法确定给定字符串是否以给定值结尾:
 
26942504   // false
26952505
26962506<a name="method-fluent-str-exactly"></a>
2697   
26982507#### `exactly` {.collection-method}
26992508
27002509`exactly` 方法确定给定的字符串是否与另一个字符串完全匹配:
 
27062515   // true
27072516
27082517<a name="method-fluent-str-explode"></a>
2709   
27102518#### `explode` {.collection-method}
27112519
27122520`explode` 方法按给定的分隔符拆分字符串并返回包含拆分字符串的每个部分的集合:
 
27182526   // collect(['foo', 'bar', 'baz'])
27192527
27202528<a name="method-fluent-str-finish"></a>
2721   
27222529#### `finish` {.collection-method}
27232530
27242531`finish` 方法将给定值的单个实例添加到字符串中(如果它尚未以该值结尾):
 
27322539   
27332540   // this/string/
27342541
2735   
2736   
27372542<a name="method-fluent-str-headline"></a>
2738   
27392543#### `headline` {.collection-method}
27402544
27412545`headline` 方法会将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串,每个单词的首字母大写:
 
27512555   // Email Notification Sent
27522556
27532557<a name="method-fluent-str-inline-markdown"></a>
2754   
27552558#### `inlineMarkdown` {.collection-method}
27562559
27572560`inlineMarkdown` 方法使用 [CommonMark](https://commonmark.thephpleague.com/) 将 GitHub 风格的 Markdown 转换为内联 HTML。 但是,与 `markdown` 方法不同,它不会将所有生成的 HTML 包装在块级元素中:
 
27632566   // <strong>Laravel</strong>
27642567
27652568<a name="method-fluent-str-is"></a>
2766   
27672569#### `is` {.collection-method}
27682570
27692571`is` 方法确定给定字符串是否与给定模式匹配。 星号可用作通配符值
 
27792581   // false
27802582
27812583<a name="method-fluent-str-is-ascii"></a>
2782   
27832584#### `isAscii` {.collection-method}
27842585
27852586`isAscii` 方法确定给定字符串是否为 ASCII 字符串:
 
27952596   // false
27962597
27972598<a name="method-fluent-str-is-empty"></a>
2798   
27992599#### `isEmpty` {.collection-method}
28002600
28012601`isEmpty` 方法确定给定的字符串是否为空:
 
28112611   // false
28122612
28132613<a name="method-fluent-str-is-not-empty"></a>
2814   
28152614#### `isNotEmpty` {.collection-method}
28162615
28172616`isNotEmpty` 方法确定给定的字符串是否不为空:
 
28262625   
28272626   // true
28282627
2829   
2830   
28312628<a name="method-fluent-str-is-json"></a>
2832   
28332629#### `isJson` {.collection-method}
28342630
28352631`isJson` 方法确定给定的字符串是否是有效的 JSON:
 
28492645   // false
28502646
28512647<a name="method-fluent-str-is-ulid"></a>
2852   
28532648#### `isUlid` {.collection-method}
28542649
28552650`isUlid` 方法确定给定的字符串是否一个 ULID:
 
28652660   // false
28662661
28672662<a name="method-fluent-str-is-uuid"></a>
2868   
28692663#### `isUuid` {.collection-method}
28702664
28712665`isUuid` 方法确定给定的字符串是否是一个 UUID:
 
28812675   // false
28822676
28832677<a name="method-fluent-str-kebab"></a>
2884   
28852678#### `kebab` {.collection-method}
28862679
28872680`kebab` 方法转变给定的字符串为 `kebab-case`:
 
29042697   
29052698   // foo Bar
29062699
2907   
29082700<a name="method-fluent-str-length"></a>
29092701
29102702#### `length` {.collection-method}
 
29182710   // 7
29192711
29202712<a name="method-fluent-str-limit"></a>
2921   
29222713#### `limit` {.collection-method}
29232714
29242715`limit` 方法将给定的字符串截断为指定的长度:
 
29292720   
29302721   // The quick brown fox...
29312722
2932   
2933   
29342723你也可以通过第二个参数来改变追加到末尾的字符串:
29352724
29362725   use Illuminate\Support\Str;
 
29402729   // The quick brown fox (...)
29412730
29422731<a name="method-fluent-str-lower"></a>
2943   
29442732#### `lower`
29452733
29462734`lower` 方法将指定字符串转换为小写:
 
29522740   // 'laravel'
29532741
29542742<a name="method-fluent-str-ltrim"></a>
2955   
29562743#### `ltrim`
29572744
29582745`ltrim` 方法移除字符串左端指定的字符:
 
29702757<a name="method-fluent-str-markdown"></a>
29712758
29722759#### `markdown` {.collection-method}
2973   
29742760`markdown` 方法将 Github 风格的 Markdown 转换为 HTML:
29752761
29762762   use Illuminate\Support\Str;
 
29862772   // <h1>Taylor Otwell</h1>
29872773
29882774<a name="method-fluent-str-mask"></a>
2989   
29902775#### `mask`
29912776
29922777`mask` 方法用重复字符掩盖字符串的一部分,可用于模糊处理字符串的某些段,例如电子邮件地址和电话号码:
 
30072792   
30082793   // tayl**********.com
30092794
3010   
3011   
30122795<a name="method-fluent-str-match"></a>
3013   
30142796#### `match`
30152797
30162798`match` 方法将会返回字符串中和指定正则表达式匹配的部分:
 
30262808   // 'bar'
30272809
30282810<a name="method-fluent-str-match-all"></a>
3029   
30302811#### `matchAll`
30312812
30322813`matchAll` 方法将会返回一个集合,该集合包含了字符串中与指定正则表达式匹配的部分
 
30482829如果没有找到任何匹配项,则返回空集合。
30492830
30502831<a name="method-fluent-str-is-match"></a>
3051   
30522832#### `isMatch`
30532833
30542834`isMatch` 方法用于判断给定的字符串是否与正则表达式匹配:
 
30642844   // false
30652845
30662846<a name="method-fluent-str-new-line"></a>
3067   
30682847#### `newLine`
30692848
30702849`newLine` 方法将给字符串追加换行的字符:
 
30772856   // Framework'
30782857
30792858<a name="method-fluent-str-padboth"></a>
3080   
30812859#### `padBoth`
30822860
30832861`padBoth` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的两侧填充上另一字符串,直至该字符串到达指定的长度:
 
30922870   
30932871   // ' James  '
30942872
3095   
3096   
30972873<a name="method-fluent-str-padleft"></a>
3098   
30992874#### `padLeft`
31002875
31012876The `padLeft` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的左侧填充上另一字符串,直至该字符串到达指定的长度:
 
31112886   // '    James'
31122887
31132888<a name="method-fluent-str-padright"></a>
3114   
31152889#### `padRight`
31162890
31172891`padRight` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的右侧填充上另一字符串,直至该字符串到达指定的长度:
 
31272901   // 'James    '
31282902
31292903<a name="method-fluent-str-pipe"></a>
3130   
31312904#### `pipe`
3132   
31332905
31342906`pipe` 方法将把字符串的当前值传递给指定的函数来转换字符串:
31352907
 
31472919   // 'bar'
31482920
31492921<a name="method-fluent-str-plural"></a>
3150   
31512922#### `plural`
31522923
3153 `plural` 方法将单数形式的字符串转换为复数形式。该此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localization#pluralization-language)
 2924`plural` 方法将单数形式的字符串转换为复数形式。该此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localizationmd#pluralization-language)
31542925
31552926   use Illuminate\Support\Str;
31562927   
 
31742945   
31752946   // child
31762947
3177   
3178   
31792948<a name="method-fluent-str-prepend"></a>
3180   
31812949#### `prepend`
31822950
31832951`prepend` 方法用于在指定字符串的开头插入另一指定字符串:
 
31892957   // Laravel Framework
31902958
31912959<a name="method-fluent-str-remove"></a>
3192   
31932960#### `remove`
31942961
31952962`remove` 方法用于从字符串中删除给定的值或值数组:
 
32032970你也可以传递 `false` 作为第二个参数以在删除字符串时忽略大小写。
32042971
32052972<a name="method-fluent-str-replace"></a>
3206   
32072973#### `replace`
32082974
32092975`replace` 方法用于将字符串中的指定字符串替换为另一指定字符串:
32102976
32112977   use Illuminate\Support\Str;
32122978   
3213    $replaced = Str::of('Laravel 6.x')->replace('6.x', '7.x');
3214    
3215    // Laravel 7.x
 2979   $replaced = Str::of('Laravel 9.x')->replace('9.x', '10.x');
 2980   
 2981   // Laravel 10.x
32162982
32172983<a name="method-fluent-str-replace-array"></a>
3218   
32192984#### `replaceArray`
32202985
32212986`replaceArray` 方法使用数组顺序替换字符串中的给定值:
 
32292994   // The event will take place between 8:30 and 9:00
32302995
32312996<a name="method-fluent-str-replace-first"></a>
3232   
32332997#### `replaceFirst`
32342998
32352999`replaceFirst` 方法替换字符串中给定值的第一个匹配项:
 
32413005   // a quick brown fox jumps over the lazy dog
32423006
32433007<a name="method-fluent-str-replace-last"></a>
3244   
32453008#### `replaceLast`
32463009
32473010`replaceLast` 方法替换字符串中给定值的最后一个匹配项:
 
32523015   
32533016   // the quick brown fox jumps over a lazy dog
32543017
3255   
3256   
32573018<a name="method-fluent-str-replace-matches"></a>
3258   
32593019#### `replaceMatches`
32603020
32613021`replaceMatches` 方法用给定的替换字符串替换与模式匹配的字符串的所有部分
 
32783038   // '[1][2][3]'
32793039
32803040<a name="method-fluent-str-rtrim"></a>
3281   
32823041#### `rtrim`
32833042
32843043`rtrim` 方法修剪给定字符串的右侧:
 
32943053   // '/Laravel'
32953054
32963055<a name="method-fluent-str-scan"></a>
3297   
32983056#### `scan`
32993057
3300 `scan` 方法根据 [PHP 函数 sscanf ](https://www.php.net/manual/en/function.sscanf.php) 支持的格式把字符串中的输入解析为集合:
 3058`scan` 方法根据 [PHP 函数 sscanf](https://www.php.net/manual/en/function.sscanf.php) 支持的格式把字符串中的输入解析为集合:
33013059
33023060   use Illuminate\Support\Str;
33033061   
 
33063064   // collect(['filename', 'jpg'])
33073065
33083066<a name="method-fluent-str-singular"></a>
3309   
33103067#### `singular`
33113068
3312 `singular` 方法将字符串转换为其单数形式。此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localization#pluralization-language) :
 3069`singular` 方法将字符串转换为其单数形式。此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localizationmd#pluralization-language) :
33133070
33143071   use Illuminate\Support\Str;
33153072   
 
33213078   
33223079   // child
33233080
3324   
3325   
33263081<a name="method-fluent-str-slug"></a>
3327   
33283082#### `slug` {.collection-method}
33293083
33303084`slug` 方法从给定字符串生成 URL 友好的 "slug":
 
33363090   // laravel-framework
33373091
33383092<a name="method-fluent-str-snake"></a>
3339   
33403093#### `snake` {.collection-method}
33413094
33423095`snake` 方法将给定字符串转换为 `snake_case`
 
33483101   // foo_bar
33493102
33503103<a name="method-fluent-str-split"></a>
3351   
33523104#### `split` {.collection-method}
33533105
33543106split 方法使用正则表达式将字符串拆分为集合:
 
33603112   // collect(["one", "two", "three"])
33613113
33623114<a name="method-fluent-str-squish"></a>
3363   
33643115#### `squish` {.collection-method}
33653116
33663117`squish` 方法删除字符串中所有无关紧要的空白,包括字符串之间的空白:
 
33723123   // laravel framework
33733124
33743125<a name="method-fluent-str-start"></a>
3375   
33763126#### `start` {.collection-method}
33773127
33783128`start` 方法将给定值的单个实例添加到字符串中,前提是该字符串尚未以该值开头:
 
33883138   // /this/string
33893139
33903140<a name="method-fluent-str-starts-with"></a>
3391   
33923141#### `startsWith` {.collection-method}
33933142
33943143`startsWith` 方法确定给定字符串是否以给定值开头:
 
34003149   // true
34013150
34023151<a name="method-fluent-str-studly"></a>
3403   
34043152#### `studly` {.collection-method}
34053153
34063154`studly` 方法将给定字符串转换为 `StudlyCase`:
 
34113159   
34123160   // FooBar
34133161
3414   
3415   
34163162<a name="method-fluent-str-substr"></a>
3417   
34183163#### `substr` {.collection-method}
34193164
34203165`substr` 方法返回由给定的起始参数和长度参数指定的字符串部分:
 
34303175   // Frame
34313176
34323177<a name="method-fluent-str-substrreplace"></a>
3433   
34343178#### `substrReplace` {.collection-method}
34353179
34363180`substrReplace` 方法在字符串的一部分中替换文本,从第二个参数指定的位置开始替换第三个参数指定的字符数。将 `0` 传递给方法的第三个参数将在指定位置插入字符串,而不替换字符串中的任何现有字符:
 
34463190   // The Laravel Framework
34473191
34483192<a name="method-fluent-str-swap"></a>
3449   
34503193#### `swap` {.collection-method}
34513194
34523195`swap` 方法使用 PHP 的 `strtr` 函数替换字符串中的多个值:
 
34803223   // LARAVEL FRAMEWORK
34813224
34823225<a name="method-fluent-str-test"></a>
3483   
3484   
34853226#### `test` {.collection-method}
34863227
34873228`test` 方法确定字符串是否与给定的正则表达式模式匹配:
 
35053246   // A Nice Title Uses The Correct Case
35063247
35073248<a name="method-fluent-str-trim"></a>
3508   
35093249#### `trim` {.collection-method}
35103250
35113251`trim` 方法修剪给定字符串:
 
35213261   // 'Laravel'
35223262
35233263<a name="method-fluent-str-ucfirst"></a>
3524   
35253264#### `ucfirst` {.collection-method}
35263265
35273266`ucfirst` 方法返回第一个字符大写的给定字符串
 
35333272   // Foo bar
35343273
35353274<a name="method-fluent-str-ucsplit"></a>
3536   
35373275#### `ucsplit` {.collection-method}
35383276
35393277`ucsplit` 方法将给定的字符串按大写字符分割为一个集合:
 
35453283   // collect(['Foo', 'Bar'])
35463284
35473285<a name="method-fluent-str-upper"></a>
3548   
35493286#### `upper` {.collection-method}
35503287
35513288`upper` 方法将给定字符串转换为大写:
 
35573294   // LARAVEL
35583295
35593296<a name="method-fluent-str-when"></a>
3560   
35613297#### `when` {.collection-method}
35623298
35633299如果给定的条件为 `true`,则 `when` 方法调用给定的闭包。闭包将接收一个流畅字符串实例:
 
35743310
35753311如果需要,可以将另一个闭包作为第三个参数传递给 `when` 方法。如果条件参数的计算结果为 `false`,则将执行此闭包。
35763312
3577   
3578   
35793313<a name="method-fluent-str-when-contains"></a>
3580   
35813314#### `whenContains` {.collection-method}
35823315
35833316`whenContains` 方法会在字符串包含给定的值的前提下,调用给定的闭包。闭包将接收字符流处理实例:
 
36073340   // Tony Stark
36083341
36093342<a name="method-fluent-str-when-contains-all"></a>
3610   
36113343#### `whenContainsAll` {.collection-method}
36123344
36133345`whenContainsAll` 方法会在字符串包含所有给定的子字符串时,调用给定的闭包。闭包将接收字符流处理实例:
 
36253357如有必要,你可以将另一个闭包作为第三个参数传递给 `whenContainsAll` 方法。如果条件参数评估为 `false`,则此闭包将执行。
36263358
36273359<a name="method-fluent-str-when-empty"></a>
3628   
36293360#### `whenEmpty` {.collection-method}
36303361
36313362如果字符串为空,`whenEmpty` 方法将调用给定的闭包。如果闭包返回一个值,`whenEmpty` 方法也将返回该值。如果闭包不返回值,则将返回字符流处理实例:
 
36393370   
36403371   // 'Laravel'
36413372
3642   
3643   
36443373<a name="method-fluent-str-when-not-empty"></a>
3645   
36463374#### `whenNotEmpty` {.collection-method}
36473375
36483376如果字符串不为空,`whenNotEmpty` 方法会调用给定的闭包。如果闭包返回一个值,那么 `whenNotEmpty` 方法也将返回该值。如果闭包没有返回值,则返回字符流处理实例:
 
36573385   // 'Laravel Framework'
36583386
36593387<a name="method-fluent-str-when-starts-with"></a>
3660   
36613388#### `whenStartsWith` {.collection-method}
36623389
36633390如果字符串以给定的子字符串开头,`whenStartsWith` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 
36723399   // 'Disney World'
36733400
36743401<a name="method-fluent-str-when-ends-with"></a>
3675   
36763402#### `whenEndsWith` {.collection-method}
36773403
36783404如果字符串以给定的子字符串结尾,`whenEndsWith` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 
36873413   // 'Disney World'
36883414
36893415<a name="method-fluent-str-when-exactly"></a>
3690   
36913416#### `whenExactly` {.collection-method}
36923417
36933418如果字符串与给定字符串完全匹配,`whenExactly` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 
37023427   // 'Laravel'
37033428
37043429<a name="method-fluent-str-when-not-exactly"></a>
3705   
37063430#### `whenNotExactly` {.collection-method}
37073431
37083432如果字符串与给定字符串不完全匹配,`whenNotExactly`方法将调用给定的闭包。闭包将接收字符流处理实例:
 
37163440   
37173441   // 'Framework'
37183442
3719   
3720   
37213443<a name="method-fluent-str-when-is"></a>
3722   
37233444#### `whenIs` {.collection-method}
37243445
37253446如果字符串匹配给定的模式,`whenIs` 方法会调用给定的闭包。星号可用作通配符值。闭包将接收字符流处理实例:
 
37343455   // 'foo/bar/baz'
37353456
37363457<a name="method-fluent-str-when-is-ascii"></a>
3737   
37383458#### `whenIsAscii` {.collection-method}
37393459
37403460如果字符串是 7 位 ASCII,`whenIsAscii` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 
37493469   // 'Laravel'
37503470
37513471<a name="method-fluent-str-when-is-ulid"></a>
3752   
37533472#### `whenIsUlid` {.collection-method}
37543473
37553474如果字符串是有效的 ULID,`whenIsUlid` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 
37633482   // '01gd6r36'
37643483
37653484<a name="method-fluent-str-when-is-uuid"></a>
3766   
37673485#### `whenIsUuid` {.collection-method}
37683486
37693487如果字符串是有效的 UUID,`whenIsUuid` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 
37783496   // 'a0a2a2d2'
37793497
37803498<a name="method-fluent-str-when-test"></a>
3781   
37823499#### `whenTest` {.collection-method}
37833500
37843501如果字符串匹配给定的正则表达式,`whenTest` 方法会调用给定的闭包。闭包将接收字符流处理实例:
 
37923509   
37933510   // 'Laravel Framework'
37943511
3795   
3796   
37973512<a name="method-fluent-str-word-count"></a>
3798   
37993513#### `wordCount` {.collection-method}
38003514
38013515`wordCount` 方法返回字符串包含的单词数:
 
38073521```
38083522
38093523<a name="method-fluent-str-words"></a>
3810   
38113524#### `words` {.collection-method}
38123525
38133526`words` 方法限制字符串中的字数。如有必要,可以指定附加到截断字符串的附加字符串:
 
38193532   // Perfectly balanced, as >>>
38203533
38213534<a name="urls"></a>
3822   
38233535## URLs
38243536
38253537<a name="method-action"></a>
3826   
38273538#### `action()` {.collection-method}
38283539
38293540`action` 函数为给定的控制器操作生成 URL:
 
38373548   $url = action([UserController::class, 'profile'], ['id' => 1]);
38383549
38393550<a name="method-asset"></a>
3840   
38413551#### `asset()` {.collection-method}
38423552
38433553`asset` 函数使用请求的当前方案(HTTP 或 HTTPS)生成 URL:
 
38543564
38553565#### `route()` {.collection-method}
38563566
3857 `route` 函数为给定的 [命名路由](/docs/laravel/10.x/routing#named-routes):
 3567`route` 函数为给定的 [命名路由](/docs/laravel/10.x/routingmd#named-routes):
38583568
38593569   $url = route('route.name');
38603570
 
38623572
38633573   $url = route('route.name', ['id' => 1]);
38643574
3865   
3866   
38673575默认情况下,`route` 函数会生成一个绝对路径的 URL。 如果想生成一个相对路径 URL,你可以传递 `false` 作为函数的第三个参数:
38683576
38693577   $url = route('route.name', ['id' => 1], false);
38703578
38713579<a name="method-secure-asset"></a>
3872   
38733580#### `secure_asset()` {.collection-method}
38743581
38753582`secure_asset` 函数使用 HTTPS 为静态资源生成 URL:
 
38773584   $url = secure_asset('img/photo.jpg');
38783585
38793586<a name="method-secure-url"></a>
3880   
38813587#### `secure_url()` {.collection-method}
38823588
38833589`secure_url` 函数生成给定路径的完全限定 HTTPS URL。 可以在函数的第二个参数中传递额外的 URL 段:
 
38873593   $url = secure_url('user/profile', [1]);
38883594
38893595<a name="method-to-route"></a>
3890   
38913596#### `to_route()` {.collection-method}
38923597
3893 `to_route` 函数为给定的[命名路由](/docs/laravel/10.x/routing#named-routes) 生成一个[重定向 HTTP 响应](/docs/laravel/10.x/responses#redirects):
 3598`to_route` 函数为给定的 [命名路由](/docs/laravel/10.x/routingmd#named-routes) 生成一个 [重定向 HTTP 响应](/docs/laravel/10.x/responsesmd#redirects):
38943599
38953600   return to_route('users.show', ['user' => 1]);
38963601
 
38993604   return to_route('users.show', ['user' => 1], 302, ['X-Framework' => 'Laravel']);
39003605
39013606<a name="method-url"></a>
3902   
39033607#### `url()` {.collection-method}
39043608
39053609`url` 函数生成给定路径的完全限定 URL:
 
39173621   $previous = url()->previous();
39183622
39193623<a name="miscellaneous"></a>
3920   
39213624## 杂项
39223625
39233626<a name="method-abort"></a>
3924   
39253627#### `abort()` {.collection-method}
39263628
3927 使用`abort` 函数抛出一个[HTTP 异常](/docs/laravel/10.x/errors#http-exceptions)交给[异常处理](/docs/laravel/10.x/errors#the-exception-handler "异常处理程序")
 3629使用`abort` 函数抛出一个 [HTTP 异常](/docs/laravel/10.x/errorsmd#http-exceptions) 交给 [异常处理](/docs/laravel/10.x/errorsmd#the-exception-handler "异常处理程序")
39283630
39293631   abort(403);
39303632
3931   
3932   
39333633你还可以提供应发送到浏览器的异常消息和自定义 HTTP 响应标头:
39343634
39353635   abort(403, 'Unauthorized.', $headers);
39363636
39373637<a name="method-abort-if"></a>
3938   
39393638#### `abort_if()` {.collection-method}
39403639
39413640如果给定的布尔表达式的计算结果为 `true`,则 `abort_if` 函数会抛出 HTTP 异常:
 
39453644与 `abort` 方法一样,你还可以提供异常的响应文本作为函数的第三个参数,并提供自定义响应标头数组作为函数的第四个参数。
39463645
39473646<a name="method-abort-unless"></a>
3948   
39493647#### `abort_unless()` {.collection-method}
39503648
39513649如果给定的布尔表达式的计算结果为 `false`,则 `abort_unless` 函数会抛出 HTTP 异常:
 
39553653与 `abort` 方法一样,你还可以提供异常的响应文本作为函数的第三个参数,并提供自定义响应标头数组作为函数的第四个参数。
39563654
39573655<a name="method-app"></a>
3958   
39593656#### `app()` {.collection-method}
39603657
3961 `app` 函数返回 [服务容器](/docs/laravel/10.x/container) 实例:
 3658`app` 函数返回 [服务容器](/docs/laravel/10.x/containermd) 实例:
39623659
39633660   $container = app();
39643661
 
39673664   $api = app('HelpSpot\API');
39683665
39693666<a name="method-auth"></a>
3970   
39713667#### `auth()` {.collection-method}
39723668
3973 `auth` 函数返回一个 [authenticator](/docs/laravel/10.x/authentication) 实例。 你可以将它用作 `Auth` 门面的替代品:
 3669`auth` 函数返回一个 [authenticator](/docs/laravel/10.x/authenticationmd) 实例。 你可以将它用作 `Auth` 门面的替代品:
39743670
39753671   $user = auth()->user();
39763672
 
39793675   $user = auth('admin')->user();
39803676
39813677<a name="method-back"></a>
3982   
39833678#### `back()` {.collection-method}
39843679
3985 `back` 函数生成一个 [重定向 HTTP 响应](/docs/laravel/10.x/responses#redirects) 到用户之前的位置:
 3680`back` 函数生成一个 [重定向 HTTP 响应](/docs/laravel/10.x/responsesmd#redirects) 到用户之前的位置:
39863681
39873682   return back($status = 302, $headers = [], $fallback = '/');
39883683   
39893684   return back();
39903685
3991   
3992   
39933686<a name="method-bcrypt"></a>
3994   
39953687#### `bcrypt()` {.collection-method}
39963688
3997 `bcrypt` 函数 [hashes](/docs/laravel/10.x/hashing) 使用 Bcrypt 的给定值。 您可以使用此函数作为 `Hash` 门面的替代方法:
 3689`bcrypt` 函数 [hashes](/docs/laravel/10.x/hashingmd) 使用 Bcrypt 的给定值。 您可以使用此函数作为 `Hash` 门面的替代方法:
39983690
39993691   $password = bcrypt('my-secret-password');
40003692
40013693<a name="method-blank"></a>
4002   
40033694#### `blank()` {.collection-method}
40043695
40053696`blank` 函数确定给定值是否为「空白」:
 
40203711对于 `blank` 的反转,请参阅 [`filled`](#method-filled) 方法。
40213712
40223713<a name="method-broadcast"></a>
4023   
40243714#### `broadcast()` {.collection-method}
40253715
4026 `broadcast` 函数 [broadcasts](/docs/laravel/10.x/broadcasting) 给定的 [event](/docs/laravel/10.x/events) 给它的听众:
 3716`broadcast` 函数 [broadcasts](/docs/laravel/10.x/broadcastingmd) 给定的 [event](/docs/laravel/10.x/eventsmd) 给它的听众:
40273717
40283718   broadcast(new UserRegistered($user));
40293719   
40303720   broadcast(new UserRegistered($user))->toOthers();
40313721
40323722<a name="method-cache"></a>
4033   
40343723#### `cache()` {.collection-method}
40353724
4036 `cache` 函数可用于从 [cache](/docs/laravel/10.x/cache) 中获取值。 如果缓存中不存在给定的键,将返回一个可选的默认值:
 3725`cache` 函数可用于从 [cache](/docs/laravel/10.x/cachemd) 中获取值。 如果缓存中不存在给定的键,将返回一个可选的默认值:
40373726
40383727   $value = cache('key');
40393728   
 
40463735   cache(['key' => 'value'], now()->addSeconds(10));
40473736
40483737<a name="method-class-uses-recursive"></a>
4049   
40503738#### `class_uses_recursive()` {.collection-method}
40513739
40523740`class_uses_recursive` 函数返回一个类使用的所有特征,包括其所有父类使用的特征:
 
40543742   $traits = class_uses_recursive(App\Models\User::class);
40553743
40563744<a name="method-collect"></a>
4057   
40583745#### `collect()` {.collection-method}
40593746
4060 `collect` 函数根据给定值创建一个 [collection](/docs/laravel/10.x/collections) 实例:
 3747`collect` 函数根据给定值创建一个 [collection](/docs/laravel/10.x/collectionsmd) 实例:
40613748
40623749   $collection = collect(['taylor', 'abigail']);
40633750
4064   
4065   
40663751<a name="method-config"></a>
4067   
40683752#### `config()` {.collection-method}
40693753
4070 `config` 函数获取 [configuration](/docs/laravel/10.x/configuration) 变量的值。 可以使用「点」语法访问配置值,其中包括文件名和你希望访问的选项。 如果配置选项不存在,可以指定默认值并返回:
 3754`config` 函数获取 [configuration](/docs/laravel/10.x/configurationmd) 变量的值。 可以使用「点」语法访问配置值,其中包括文件名和你希望访问的选项。 如果配置选项不存在,可以指定默认值并返回:
40713755
40723756   $value = config('app.timezone');
40733757   
 
40783762   config(['app.debug' => true]);
40793763
40803764<a name="method-cookie"></a>
4081   
40823765#### `cookie()` {.collection-method}
40833766
4084 `cookie` 函数创建一个新的 [cookie](/docs/laravel/10.x/requests#cookies) 实例:
 3767`cookie` 函数创建一个新的 [cookie](/docs/laravel/10.x/requestsmd#cookies) 实例:
40853768
40863769   $cookie = cookie('name', 'value', $minutes);
40873770
40883771<a name="method-csrf-field"></a>
4089   
40903772#### `csrf_field()` {.collection-method}
40913773
4092 `csrf_field` 函数生成一个 HTML `hidden` 输入字段,其中包含 CSRF 令牌的值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blade):
 3774`csrf_field` 函数生成一个 HTML `hidden` 输入字段,其中包含 CSRF 令牌的值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blademd):
40933775
40943776   {{ csrf_field() }}
40953777
40963778<a name="method-csrf-token"></a>
4097   
40983779#### `csrf_token()` {.collection-method}
40993780
41003781`csrf_token` 函数检索当前 CSRF 令牌的值:
 
41023783   $token = csrf_token();
41033784
41043785<a name="method-decrypt"></a>
4105   
41063786#### `decrypt()` {.collection-method}
41073787
4108 `decrypt` 函数 [解密](/docs/laravel/10.x/encryption) 给定的值。 你可以使用此函数作为 `Crypt` 门面的替代方法:
 3788`decrypt` 函数 [解密](/docs/laravel/10.x/encryptionmd) 给定的值。 你可以使用此函数作为 `Crypt` 门面的替代方法:
41093789
41103790   $password = decrypt($value);
41113791
41123792<a name="method-dd"></a>
4113   
41143793#### `dd()` {.collection-method}
41153794
41163795`dd` 函数转储给定的变量并结束脚本的执行:
 
41213800
41223801如果你不想停止脚本的执行,请改用 [`dump`](#method-dump) 函数。
41233802
4124   
41253803<a name="method-dispatch"></a>
4126   
41273804#### `dispatch()` {.collection-method}
41283805
4129 `dispatch` 函数将给定的 [job](/docs/laravel/10.x/queues#creating-jobs) 推送到 Laravel [job queue](/docs/laravel/10.x/queues):
 3806`dispatch` 函数将给定的 [job](/docs/laravel/10.x/queuesmd#creating-jobs) 推送到 Laravel [job queue](/docs/laravel/10.x/queuesmd):
41303807
41313808   dispatch(new App\Jobs\SendEmails);
41323809
41333810<a name="method-dump"></a>
4134   
41353811#### `dump()` {.collection-method}
41363812
41373813`dump` 函数转储给定的变量:
 
41433819如果要在转储变量后停止执行脚本,请改用 [`dd`](#method-dd) 函数。
41443820
41453821<a name="method-encrypt"></a>
4146   
41473822#### `encrypt()` {.collection-method}
41483823
4149 `encrypt` 函数 [encrypts](/docs/laravel/10.x/encryption) 给定值。 你可以使用此函数作为 `Crypt` 门面的替代方法:
 3824`encrypt` 函数 [encrypts](/docs/laravel/10.x/encryptionmd) 给定值。 你可以使用此函数作为 `Crypt` 门面的替代方法:
41503825
41513826   $secret = encrypt('my-secret-value');
41523827
41533828<a name="method-env"></a>
4154   
41553829#### `env()` {.collection-method}
41563830
4157 `env` 函数检索 [环境变量](/docs/laravel/10.x/configuration#environment-configuration) 的值或返回默认值:
 3831`env` 函数检索 [环境变量](/docs/laravel/10.x/configurationmd#environment-configuration) 的值或返回默认值:
41583832
41593833   $env = env('APP_ENV');
41603834   
 
41643838> 如果你在部署过程中执行 `config:cache` 命令,你应该确保只从配置文件中调用 `env` 函数。 一旦配置被缓存,`.env` 文件将不会被加载,所有对 `env` 函数的调用都将返回 `null`。
41653839
41663840<a name="method-event"></a>
4167   
41683841#### `event()` {.collection-method}
41693842
4170 `event` 函数将给定的 [event](/docs/laravel/10.x/events) 分派给它的监听器:
 3843`event` 函数将给定的 [event](/docs/laravel/10.x/eventsmd) 分派给它的监听器:
41713844
41723845   event(new UserRegistered($user));
41733846
41743847<a name="method-fake"></a>
4175   
41763848#### `fake()` {.collection-method}
41773849
41783850`fake` 函数解析容器中的 [Faker](https://github.com/FakerPHP/Faker) 单例,这在模型工厂、数据库填充、测试和原型视图中创建假数据时非常有用:
 
41893861@endfor
41903862```
41913863
4192   
4193   
41943864默认情况下,`fake` 函数将使用 `config/app.php` 配置文件中的 `app.faker_locale` 配置选项; 但是,你也可以通过将语言环境传递给 `fake` 函数来指定语言环境。 每个语言环境将解析一个单独的单例:
41953865
41963866   fake('nl_NL')->name()
41973867
41983868<a name="method-filled"></a>
4199   
42003869#### `filled()` {.collection-method}
42013870
42023871`filled` 函数确定给定值是否不是「空白」:
 
42173886对于 `filled` 的反转,请参阅 [`blank`](#method-blank) 方法。
42183887
42193888<a name="method-info"></a>
4220   
42213889#### `info()` {.collection-method}
42223890
4223 `info` 函数会将信息写入应用程序的 [log](/docs/laravel/10.x/logging):
 3891`info` 函数会将信息写入应用程序的 [log](/docs/laravel/10.x/loggingmd):
42243892
42253893   info('Some helpful information!');
42263894
 
42293897   info('User login attempt failed.', ['id' => $user->id]);
42303898
42313899<a name="method-logger"></a>
4232   
42333900#### `logger()` {.collection-method}
42343901
4235 `logger` 函数可用于将 `debug` 级别的消息写入 [log](/docs/laravel/10.x/logging):
 3902`logger` 函数可用于将 `debug` 级别的消息写入 [log](/docs/laravel/10.x/loggingmd):
42363903
42373904   logger('Debug message');
42383905
 
42403907
42413908   logger('User has logged in.', ['id' => $user->id]);
42423909
4243 如果没有值传递给函数,将返回一个 [logger](/docs/laravel/10.x/errors#logging) 实例:
 3910如果没有值传递给函数,将返回一个 [logger](/docs/laravel/10.x/errorsmd#logging) 实例:
42443911
42453912   logger()->error('You are not allowed here.');
42463913
42473914<a name="method-method-field"></a>
4248   
42493915#### `method_field()` {.collection-method}
42503916
4251 `method_field` 函数生成一个 HTML `hidden` 输入字段,其中包含表单 HTTP 谓词的欺骗值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blade):
 3917`method_field` 函数生成一个 HTML `hidden` 输入字段,其中包含表单 HTTP 谓词的欺骗值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blademd):
42523918
42533919   <form method="POST">
42543920       {{ method_field('DELETE') }}
42553921   </form>
42563922
42573923<a name="method-now"></a>
4258   
4259   
42603924#### `now()` {.collection-method}
42613925
42623926`now` 函数为当前时间创建一个新的 `Illuminate\Support\Carbon` 实例:
 
42643928   $now = now();
42653929
42663930<a name="method-old"></a>
4267   
42683931#### `old()` {.collection-method}
42693932
4270 `old` 函数 [retrieves](/docs/laravel/10.x/requests#retrieving-input) 一个 [old input](/docs/laravel/10.x/requests#old-input) 值闪入Session :
 3933`old` 函数 [retrieves](/docs/laravel/10.x/requestsmd#retrieving-input) 一个 [old input](/docs/laravel/10.x/requestsmd#old-input) 值闪入Session :
42713934
42723935   $value = old('value');
42733936   
 
42823945   {{ old('name', $user) }}
42833946
42843947<a name="method-optional"></a>
4285   
42863948#### `optional()` {.collection-method}
42873949
42883950`optional` 函数接受任何参数并允许您访问该对象的属性或调用方法。 如果给定对象为「null」,属性和方法将返回「null」而不是导致错误:
 
42983960   });
42993961
43003962<a name="method-policy"></a>
4301   
43023963#### `policy()` {.collection-method}
43033964
4304 `policy` 方法检索给定类的 [policy](/docs/laravel/10.x/authorization#creating-policies) 实例:
 3965`policy` 方法检索给定类的 [policy](/docs/laravel/10.x/authorizationmd#creating-policies) 实例:
43053966
43063967   $policy = policy(App\Models\User::class);
43073968
43083969<a name="method-redirect"></a>
4309   
43103970#### `redirect()` {.collection-method}
43113971
4312 
4313 
4314 `redirect` 函数返回一个[重定向 HTTP 响应](/docs/laravel/10.x/responses#redirects),或者如果不带参数调用则返回重定向器实例:
 3972`redirect` 函数返回一个[重定向 HTTP 响应](/docs/laravel/10.x/responsesmd#redirects),或者如果不带参数调用则返回重定向器实例:
43153973
43163974   return redirect($to = null, $status = 302, $headers = [], $https = null);
43173975   
 
43203978   return redirect()->route('route.name');
43213979
43223980<a name="method-report"></a>
4323   
43243981#### `report()` {.collection-method}
43253982
4326 `report` 函数将使用您的 [异常处理程序](/docs/laravel/10.x/errors#the-exception-handler) 报告异常:
 3983`report` 函数将使用您的 [异常处理程序](/docs/laravel/10.x/errorsmd#the-exception-handler) 报告异常:
43273984
43283985   report($e);
43293986
 
43323989   report('Something went wrong.');
43333990
43343991<a name="method-report-if"></a>
4335   
43363992#### `report_if()` {.collection-method}
43373993
4338 如果给定条件为「真」,「report_if」函数将使用您的[异常处理程序](/docs/laravel/10.x/errors#the-exception-handler) 报告异常:
 3994如果给定条件为「true」,「report_if」函数将使用您的 [异常处理程序](/docs/laravel/10.x/errorsmd#the-exception-handler) 报告异常:
43393995
43403996   report_if($shouldReport, $e);
43413997   
43423998   report_if($shouldReport, 'Something went wrong.');
43433999
43444000<a name="method-report-unless"></a>
4345   
43464001#### `report_unless()` {.collection-method}
43474002
4348 如果给定条件为 `false`,`report_unless` 函数将使用你的 [异常处理程序](/docs/laravel/10.x/errors#the-exception-handler) 报告异常:
 4003如果给定条件为 `false`,`report_unless` 函数将使用你的 [异常处理程序](/docs/laravel/10.x/errorsmd#the-exception-handler) 报告异常:
43494004
43504005   report_unless($reportingDisabled, $e);
43514006   
43524007   report_unless($reportingDisabled, 'Something went wrong.');
43534008
43544009<a name="method-request"></a>
4355   
43564010#### `request()` {.collection-method}
43574011
4358 `request` 函数返回当前的 [request](/docs/laravel/10.x/requests) 实例或从当前请求中获取输入字段的值:
 4012`request` 函数返回当前的 [request](/docs/laravel/10.x/requestsmd) 实例或从当前请求中获取输入字段的值:
43594013
43604014   $request = request();
43614015   
43624016   $value = request('key', $default);
43634017
43644018<a name="method-rescue"></a>
4365   
43664019#### `rescue()` {.collection-method}
43674020
4368 `rescue` 函数执行给定的闭包并捕获其执行期间发生的任何异常。 捕获的所有异常都将发送到你的[异常处理程序](/docs/laravel/10.x/errors#the-exception-handler); 但是,请求将继续处理:
 4021`rescue` 函数执行给定的闭包并捕获其执行期间发生的任何异常。 捕获的所有异常都将发送到你的[异常处理程序](/docs/laravel/10.x/errorsmd#the-exception-handler); 但是,请求将继续处理:
43694022
43704023   return rescue(function () {
43714024       return $this->method();
43724025   });
4373   
4374   
43754026
43764027你还可以将第二个参数传递给「rescue」函数。 如果在执行闭包时发生异常,这个参数将是应该返回的「默认」值:
43774028
 
43864037   });
43874038
43884039<a name="method-resolve"></a>
4389   
43904040#### `resolve()` {.collection-method}
43914041
4392 `resolve` 函数使用 [服务容器](/docs/laravel/10.x/container) 将给定的类或接口名称解析为实例:
 4042`resolve` 函数使用 [服务容器](/docs/laravel/10.x/containermd) 将给定的类或接口名称解析为实例:
43934043
43944044   $api = resolve('HelpSpot\API');
43954045
43964046<a name="method-response"></a>
4397   
43984047#### `response()` {.collection-method}
43994048
4400 `response` 函数创建一个 [response](/docs/laravel/10.x/responses) 实例或获取响应工厂的实例:
 4049`response` 函数创建一个 [response](/docs/laravel/10.x/responsesmd) 实例或获取响应工厂的实例:
44014050
44024051   return response('Hello World', 200, $headers);
44034052   
44044053   return response()->json(['foo' => 'bar'], 200, $headers);
44054054
44064055<a name="method-retry"></a>
4407   
44084056#### `retry()` {.collection-method}
44094057
44104058`retry` 函数尝试执行给定的回调,直到达到给定的最大尝试阈值。 如果回调没有抛出异常,则返回它的返回值。 如果回调抛出异常,它会自动重试。 如果超过最大尝试次数,将抛出异常:
 
44294077       // 第一次重试时休眠 100 ms,第二次重试时休眠 200 ms...
44304078   });
44314079
4432   
4433   
44344080要仅在特定条件下重试,您可以将闭包作为第四个参数传递给 `retry` 函数:
44354081
44364082   use Exception;
 
44424088   });
44434089
44444090<a name="method-session"></a>
4445   
44464091#### `session()` {.collection-method}
44474092
4448 `session` 函数可用于获取或设置 [session](/docs/laravel/10.x/session) 值:
 4093`session` 函数可用于获取或设置 [session](/docs/laravel/10.x/sessionmd) 值:
44494094
44504095   $value = session('key');
44514096
 
44604105   session()->put('key', $value);
44614106
44624107<a name="method-tap"></a>
4463   
44644108#### `tap()` {.collection-method}
44654109
44664110`tap` 函数接受两个参数:一个任意的 `$value` 和一个闭包。 `$value` 将传递给闭包,然后由 `tap` 函数返回。 闭包的返回值是无关紧要的:
 
44844128       // ...
44854129   });
44864130
4487   
4488   
44894131<a name="method-throw-if"></a>
4490   
44914132#### `throw_if()` {.collection-method}
44924133
44934134如果给定的布尔表达式的计算结果为「真」,则 `throw_if` 函数会抛出给定的异常:
 
44974138   throw_if(
44984139       ! Auth::user()->isAdmin(),
44994140       AuthorizationException::class,
4500        'You are not allowed to access this page.'
 4141       '你不允许访问此页面。'
45014142   );
45024143
45034144<a name="method-throw-unless"></a>
4504   
45054145#### `throw_unless()` {.collection-method}
45064146
45074147如果给定的布尔表达式的计算结果为 `false`,则 `throw_unless` 函数会抛出给定的异常:
 
45114151   throw_unless(
45124152       Auth::user()->isAdmin(),
45134153       AuthorizationException::class,
4514        'You are not allowed to access this page.'
 4154       '你不允许访问此页面。'
45154155   );
45164156
45174157<a name="method-today"></a>
4518   
45194158#### `today()` {.collection-method}
45204159
45214160如果给定的布尔表达式的计算结果为 `false`,则 `throw_unless` 函数会抛出给定的异常:
 
45234162   $today = today();
45244163
45254164<a name="method-trait-uses-recursive"></a>
4526   
45274165#### `trait_uses_recursive()` {.collection-method}
45284166
45294167`trait_uses_recursive` 函数返回特征使用的所有 trait:
 
45314169   $traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class);
45324170
45334171<a name="method-transform"></a>
4534   
45354172#### `transform()` {.collection-method}
45364173
45374174如果值不是 [blank](#method-blank),则 transform 函数会对给定值执行闭包,然后返回闭包的返回值:
 
45514188   // The value is blank
45524189
45534190<a name="method-validator"></a>
4554   
45554191#### `validator()` {.collection-method}
45564192
4557 `validator` 函数使用给定的参数创建一个新的 [validator](/docs/laravel/10.x/validation) 实例。 你可以将它用作 `Validator` 门面的替代品:
 4193`validator` 函数使用给定的参数创建一个新的 [validator](/docs/laravel/10.x/validationmd) 实例。 你可以将它用作 `Validator` 门面的替代品:
45584194
45594195   $validator = validator($data, $rules, $messages);
4560   
4561   
45624196
45634197<a name="method-value"></a>
45644198
 
45854219   // 'Taylor'
45864220
45874221<a name="method-view"></a>
4588   
45894222#### `view()` {.collection-method}
45904223
4591 `view` 函数检索一个 [view](/docs/laravel/10.x/views) 实例:
 4224`view` 函数检索一个 [view](/docs/laravel/10.x/viewsmd) 实例:
45924225
45934226   return view('auth.login');
45944227
45954228<a name="method-with"></a>
4596   
45974229#### `with()` {.collection-method}
45984230
45994231`with` 函数返回给定的值。 如果将闭包作为函数的第二个参数传递,则将执行闭包并返回其返回值:
 
46154247   // 5
46164248
46174249<a name="other-utilities"></a>
4618   
46194250## 其他
46204251
46214252<a name="benchmarking"></a>
4622   
46234253### 基准测试
46244254
46254255有时你可能希望快速测试应用程序某些部分的性能。 在这些情况下,您可以使用 Benchmark 支持类来测量给定回调完成所需的毫秒数:
 
46384268
46394269默认情况下,给定的回调将执行一次(一次迭代),并且它们的持续时间将显示在浏览器/控制台中。
46404270
4641   
46424271要多次调用回调,你可以将回调应调用的迭代次数指定为方法的第二个参数。 当多次执行回调时,「基准」类将返回在所有迭代中执行回调所花费的平均毫秒数:
46434272
46444273   Benchmark::dd(fn () => User::count(), iterations: 10); // 0.5 ms
46454274
46464275<a name="pipeline"></a>
4647   
46484276### 管道
46494277
46504278Laravel 的 Pipeline 门面提供了一种便捷的方式来通过一系列可调用类、闭包或可调用对象「管道」给定输入,让每个类都有机会检查或修改输入并调用管道中的下一个可调用对象:
46514279
4652 ```php
4653 use Closure;
4654 use App\Models\User;
4655 use Illuminate\Support\Facades\Pipeline;
4656 
4657 $user = Pipeline::send($user)
 4280   use Closure;
 4281   use App\Models\User;
 4282   use Illuminate\Support\Facades\Pipeline;
 4283
 4284   $user = Pipeline::send($user)
46584285           ->through([
46594286               function (User $user, Closure $next) {
46604287                   // ...
 
46684295               },
46694296           ])
46704297           ->then(fn (User $user) => $user);
4671 ```
4672 
4673 如你所见,管道中的每个可调用类或闭包都提供了输入和一个 `$next` 闭包。 调用 `$next` 闭包将调用管道中的下一个可调用对象。 你可能已经注意到,这与 [middleware](/docs/laravel/10.x/middleware) 非常相似。
 4298
 4299如你所见,管道中的每个可调用类或闭包都提供了输入和一个 `$next` 闭包。 调用 `$next` 闭包将调用管道中的下一个可调用对象。 你可能已经注意到,这与 [middleware](/docs/laravel/10.x/middlewaremd) 非常相似。
46744300
46754301当管道中的最后一个可调用对象调用 `$next` 闭包时,提供给 `then` 方法的可调用对象将被调用。 通常,此可调用对象将简单地返回给定的输入。
46764302
4677 当然,如前所述,你不仅限于为管道提供闭包。 你还可以提供可调用的类。 如果提供了类名,该类将通过 Laravel 的 [服务容器](/docs/laravel/10.x/container) 实例化,允许将依赖项注入可调用类:
4678 
4679 ```php
4680 $user = Pipeline::send($user)
 4303当然,如前所述,你不仅限于为管道提供闭包。 你还可以提供可调用的类。 如果提供了类名,该类将通过 Laravel 的 [服务容器](/docs/laravel/10.x/containermd) 实例化,允许将依赖项注入可调用类:
 4304
 4305   $user = Pipeline::send($user)
46814306           ->through([
46824307               GenerateProfilePhoto::class,
46834308               ActivateSubscription::class,
46844309               SendWelcomeEmail::class,
46854310           ])
46864311           ->then(fn (User $user) => $user);
4687 ```  
4688   
4689   
46904312
46914313<a name="lottery"></a>
4692   
46934314### 彩票
46944315
46954316Laravel 的 Lottery 类可用于根据一组给定的赔率执行回调。 当你只想为一定比例的传入请求执行代码时,这会特别有用:
 
47134334   );
47144335
47154336<a name="testing-lotteries"></a>
4716   
47174337#### 测试彩票
47184338
47194339Laravel 提供了一些简单的方法来让你轻松测试应用程序的 Lottery 调用: