校对内容

修改理由:
相关信息:
- 类型:文档文章
- 文章: 辅助函数
- 文档: 《Laravel 10 中文文档(10.x)》
此投稿由 神的孩子丶都在跳舞 在 1年前 合并。
标题修改:
内容修改:
Old | New | Differences |
---|---|---|
8 | 8 | - [Lottery](#lottery) |
9 | 9 | |
10 | 10 | <a name="introduction"></a> |
11 | ||
12 | 11 | ## 简介 |
13 | 12 | |
14 | 13 | Laravel 包含各种各样的全局 PHP 「辅助」函数,框架本身也大量的使用了这些功能函数;如果你觉的方便,你可以在你的应用中任意使用这些函数。 |
15 | 14 | |
16 | 15 | <a name="available-methods"></a> |
17 | ||
18 | 16 | ## 可用方法 |
19 | 17 | |
20 | 18 | <style> |
21 | 19 | .collection-method-list > p { |
22 | 20 | columns: 10.8em 3; -moz-columns: 10.8em 3; -webkit-columns: 10.8em 3; |
23 | 21 | } |
24 | ||
25 | 22 | |
26 | 23 | .collection-method-list a { |
27 | 24 | display: block; | … | … |
33 | 30 | </style> |
34 | 31 | |
35 | 32 | <a name="arrays-and-objects-method-list"></a> |
36 | ||
37 | 33 | ### 数组 & 对象 |
38 | 34 | |
39 | 35 | <div class="collection-method-list" markdown="1"> |
40 | ||
41 | 36 | |
42 | 37 | [Arr::accessible](#method-array-accessible) |
43 | 38 | [Arr::add](#method-array-add) | … | … |
81 | 76 | [data_set](#method-data-set) |
82 | 77 | [head](#method-head) |
83 | 78 | [last](#method-last) |
79 | ||
84 | 80 | </div> |
85 | 81 | |
86 | 82 | <a name="paths-method-list"></a> |
87 | ||
88 | 83 | ### 路径 |
89 | 84 | |
90 | 85 | <div class="collection-method-list" markdown="1"> |
91 | ||
92 | 86 | |
93 | 87 | [app_path](#method-app-path) |
94 | 88 | [base_path](#method-base-path) | … | … |
102 | 96 | |
103 | 97 | </div> |
104 | 98 | |
105 | ||
106 | ||
107 | 99 | <a name="strings-method-list"></a> |
108 | ||
109 | 100 | ### 字符串 |
110 | 101 | |
111 | 102 | <div class="collection-method-list" markdown="1"> |
112 | ||
113 | 103 | |
114 | 104 | [\__](#method-__) |
115 | 105 | [class_basename](#method-class-basename) | … | … |
183 | 173 | </div> |
184 | 174 | |
185 | 175 | <a name="fluent-strings-method-list"></a> |
186 | ||
187 | 176 | ### 字符流处理 |
188 | 177 | |
189 | 178 | <div class="collection-method-list" markdown="1"> |
190 | ||
191 | 179 | |
192 | 180 | [after](#method-fluent-str-after) |
193 | 181 | [afterLast](#method-fluent-str-after-last) | … | … |
280 | 268 | |
281 | 269 | </div> |
282 | 270 | |
283 | ||
284 | ||
285 | 271 | <a name="urls-method-list"></a> |
286 | ||
287 | 272 | ### URLs |
288 | 273 | |
289 | 274 | <div class="collection-method-list" markdown="1"> |
290 | ||
291 | 275 | |
292 | 276 | [action](#method-action) |
293 | 277 | [asset](#method-asset) | … | … |
300 | 284 | </div> |
301 | 285 | |
302 | 286 | <a name="miscellaneous-method-list"></a> |
303 | ||
304 | 287 | ### 杂项 |
305 | 288 | |
306 | 289 | <div class="collection-method-list" markdown="1"> |
307 | ||
308 | 290 | |
309 | 291 | [abort](#method-abort) |
310 | 292 | [abort_if](#method-abort-if) | … | … |
362 | 344 | </div> |
363 | 345 | |
364 | 346 | <a name="method-listing"></a> |
365 | ||
366 | 347 | ## 方法列表 |
367 | 348 | |
368 | 349 | <style> | … | … |
370 | 351 | font-size: 14px; |
371 | 352 | } |
372 | 353 | |
373 | ||
374 | 354 | .collection-method:not(.first-collection-method) { |
375 | 355 | margin-top: 50px; |
376 | 356 | } | … | … |
378 | 358 | </style> |
379 | 359 | |
380 | 360 | <a name="arrays"></a> |
381 | ||
382 | 361 | ## 数组 & 对象 |
383 | 362 | |
384 | 363 | <a name="method-array-accessible"></a> |
385 | ||
386 | 364 | #### `Arr::accessible()` {.collection-method .first-collection-method} |
387 | 365 | |
388 | 366 | `Arr::accessible` 方法检查给定的值是否可被数组式访问: | … | … |
407 | 385 | // false |
408 | 386 | |
409 | 387 | <a name="method-array-add"></a> |
410 | ||
411 | 388 | #### `Arr::add()` {.collection-method} |
412 | 389 | |
413 | 390 | 如果给定的键名在数组中不存在键值或该键值设置为 `null` ,那么 `Arr::add` 方法将会把给定的键值对添加到数组中: | … | … |
422 | 399 | |
423 | 400 | // ['name' => 'Desk', 'price' => 100] |
424 | 401 | |
425 | ||
426 | ||
427 | ||
428 | 402 | <a name="method-array-collapse"></a> |
429 | ||
430 | 403 | #### `Arr::collapse()` {.collection-method} |
431 | 404 | |
432 | 405 | `Arr::collapse` 方法将多个数组合并为一个数组: | … | … |
438 | 411 | // [1, 2, 3, 4, 5, 6, 7, 8, 9] |
439 | 412 | |
440 | 413 | <a name="method-array-crossjoin"></a> |
441 | ||
442 | 414 | #### `Arr::crossJoin()` {.collection-method} |
443 | 415 | |
444 | 416 | `Arr::crossJoin` 方法交叉连接给定的数组,返回具有所有可能排列的笛卡尔乘积: | … | … |
472 | 444 | */ |
473 | 445 | |
474 | 446 | <a name="method-array-divide"></a> |
475 | ||
476 | 447 | #### `Arr::divide()` {.collection-method} |
477 | 448 | |
478 | 449 | `Arr::divide` 方法返回一个二维数组,一个值包含原数组的键,另一个值包含原数组的值: | … | … |
486 | 457 | // $values: ['Desk'] |
487 | 458 | |
488 | 459 | <a name="method-array-dot"></a> |
489 | ||
490 | 460 | #### `Arr::dot()` {.collection-method} |
491 | 461 | |
492 | 462 | `Arr::dot` 方法将多维数组中所有的键平铺到一维数组中,新数组使用「.」符号表示层级包含关系: | … | … |
500 | 470 | // ['products.desk.price' => 100] |
501 | 471 | |
502 | 472 | <a name="method-array-except"></a> |
503 | ||
504 | 473 | #### `Arr::except()` {.collection-method} |
505 | 474 | |
506 | 475 | `Arr::except` 方法从数组中删除指定的键值对: | … | … |
514 | 483 | // ['name' => 'Desk'] |
515 | 484 | |
516 | 485 | <a name="method-array-exists"></a> |
517 | ||
518 | 486 | #### `Arr::exists()` {.collection-method} |
519 | 487 | |
520 | 488 | `Arr::exists` 方法检查给定的键是否存在提供的数组中: | … | … |
532 | 500 | // false |
533 | 501 | |
534 | 502 | <a name="method-array-first"></a> |
535 | ||
536 | ||
537 | 503 | #### `Arr::first()` {.collection-method} |
538 | 504 | |
539 | 505 | `Arr::first` 方法返回数组中满足指定条件的第一个元素: | … | … |
569 | 535 | // ['Joe', 'PHP', 'Ruby'] |
570 | 536 | |
571 | 537 | <a name="method-array-forget"></a> |
572 | ||
573 | 538 | #### `Arr::forget()` {.collection-method} |
574 | 539 | |
575 | 540 | `Arr::forget` 方法使用「.」符号从深度嵌套的数组中删除给定的键值对: | … | … |
583 | 548 | // ['products' => []] |
584 | 549 | |
585 | 550 | <a name="method-array-get"></a> |
586 | ||
587 | 551 | #### `Arr::get()` {.collection-method} |
588 | 552 | |
589 | 553 | `Arr::get` 方法使用「.」符号从深度嵌套的数组中根据指定键检索值: | … | … |
605 | 569 | // 0 |
606 | 570 | |
607 | 571 | <a name="method-array-has"></a> |
608 | ||
609 | 572 | #### `Arr::has()` {.collection-method} |
610 | 573 | |
611 | 574 | `Arr::has` 方法使用「.」符号判断数组中是否存在指定的一个或多个键: | … | … |
622 | 585 | |
623 | 586 | // false |
624 | 587 | |
625 | ||
626 | ||
627 | 588 | <a name="method-array-hasany"></a> |
628 | ||
629 | 589 | #### `Arr::hasAny()` {.collection-method} |
630 | 590 | |
631 | 591 | `Arr::hasAny` 方法使用「.」符号判断给定集合中的任一值是否存在于数组中: | … | … |
647 | 607 | // false |
648 | 608 | |
649 | 609 | <a name="method-array-isassoc"></a> |
650 | ||
651 | 610 | #### `Arr::isAssoc()` {.collection-method} |
652 | 611 | |
653 | 612 | 如果给定数组是关联数组,则 `Arr::isAssoc` 方法返回 `true`,如果该数组没有以零开头的顺序数字键,则将其视为「关联」数组: | … | … |
663 | 622 | // false |
664 | 623 | |
665 | 624 | <a name="method-array-islist"></a> |
666 | ||
667 | 625 | #### `Arr::isList()` {.collection-method} |
668 | 626 | |
669 | 627 | 如果给定数组的键是从零开始的连续整数,则 `Arr::isList` 方法返回 `true`: | … | … |
679 | 637 | // false |
680 | 638 | |
681 | 639 | <a name="method-array-join"></a> |
682 | ||
683 | 640 | #### `Arr::join()` {.collection-method} |
684 | 641 | |
685 | 642 | `Arr::join()`方法将给定数组的所有值通过给定字符串连接起来。使用此方法的第二个参数,您还可以为数组中的最后一个元素指定连接的字符串: | … | … |
697 | 654 | // Tailwind, Alpine, Laravel and Livewire |
698 | 655 | |
699 | 656 | <a name="method-array-keyby"></a> |
700 | ||
701 | 657 | #### `Arr::keyBy()` {.collection-method} |
702 | 658 | |
703 | 659 | `Arr::keyBy()`方法通过给定键名的值对该数组进行重组。如果数组中存在多个相同的值,则只有最后一个值会出现在新数组中: | … | … |
718 | 674 | ] |
719 | 675 | */ |
720 | 676 | |
721 | ||
722 | ||
723 | 677 | <a name="method-array-last"></a> |
724 | ||
725 | 678 | #### `Arr::last()` {.collection-method} |
726 | 679 | |
727 | 680 | `Arr::last` 方法返回数组中满足指定条件的最后一个元素: | … | … |
743 | 696 | $last = Arr::last($array, $callback, $default); |
744 | 697 | |
745 | 698 | <a name="method-array-map"></a> |
746 | ||
747 | 699 | #### `Arr::map()` {.collection-method} |
748 | 700 | |
749 | 701 | `Arr::map` 方法用来遍历数组,并将每个值和键传递给给定的回调。数组值由回调返回的值替换: | … | … |
759 | 711 | // ['first' => 'James', 'last' => 'Kirk'] |
760 | 712 | |
761 | 713 | <a name="method-array-only"></a> |
762 | ||
763 | 714 | #### `Arr::only()` {.collection-method} |
764 | 715 | |
765 | 716 | `Arr::only` 方法仅返回给定数组中的指定键/值对: | … | … |
773 | 724 | // ['name' => 'Desk', 'price' => 100] |
774 | 725 | |
775 | 726 | <a name="method-array-pluck"></a> |
776 | ||
777 | 727 | #### `Arr::pluck()` {.collection-method} |
778 | 728 | |
779 | 729 | `Arr::pluck` 方法从数组中检索给定键的所有值: | … | … |
799 | 749 | |
800 | 750 | <a name="method-array-prepend"></a> |
801 | 751 | |
802 | ||
803 | 752 | <a name="method-array-last"></a> |
804 | ||
805 | 753 | #### `Arr::last()` {.collection-method} |
806 | 754 | |
807 | 755 | `Arr::last` 方法返回数组中满足指定条件的最后一个元素: | … | … |
823 | 771 | $last = Arr::last($array, $callback, $default); |
824 | 772 | |
825 | 773 | <a name="method-array-map"></a> |
826 | ||
827 | 774 | #### `Arr::map()` {.collection-method} |
828 | 775 | |
829 | 776 | `Arr::map` 方法遍历数组并将每个键和值传递至给定的回调方法。数组的值将替换为该回调方法返回的值: | … | … |
839 | 786 | // ['first' => 'James', 'last' => 'Kirk'] |
840 | 787 | |
841 | 788 | <a name="method-array-only"></a> |
842 | ||
843 | 789 | #### `Arr::only()` {.collection-method} |
844 | 790 | |
845 | 791 | `Arr::only` 方法只返回给定数组中指定的键值对: | … | … |
853 | 799 | // ['name' => 'Desk', 'price' => 100] |
854 | 800 | |
855 | 801 | <a name="method-array-pluck"></a> |
856 | ||
857 | 802 | #### `Arr::pluck()` {.collection-method} |
858 | 803 | |
859 | 804 | `Arr::pluck` 方法从数组中检索给定键的所有值: | … | … |
878 | 823 | // [1 => 'Taylor', 2 => 'Abigail'] |
879 | 824 | |
880 | 825 | <a name="method-array-prepend"></a> |
881 | ||
882 | ||
883 | 826 | #### `Arr::prepend()` {.collection-method} |
884 | 827 | |
885 | 828 | `Arr::prepend` 方法将一个值插入到数组的开始位置: | … | … |
903 | 846 | // ['name' => 'Desk', 'price' => 100] |
904 | 847 | |
905 | 848 | <a name="method-array-prependkeyswith"></a> |
906 | ||
907 | 849 | #### `Arr::prependKeysWith()` {.collection-method} |
908 | 850 | |
909 | 851 | `Arr::prependKeysWith` 方法为关联数组中的所有键添加给定前缀: | … | … |
925 | 867 | */ |
926 | 868 | |
927 | 869 | <a name="method-array-pull"></a> |
928 | ||
929 | 870 | #### `Arr::pull()` {.collection-method} |
930 | 871 | |
931 | 872 | `Arr::pull` 方法从数组中返回指定键的值并删除此键值对: | … | … |
947 | 888 | $value = Arr::pull($array, $key, $default); |
948 | 889 | |
949 | 890 | <a name="method-array-query"></a> |
950 | ||
951 | 891 | #### `Arr::query()` {.collection-method} |
952 | 892 | |
953 | 893 | `Arr::query` 方法将数组转换为查询字符串: | … | … |
967 | 907 | // name=Taylor&order[column]=created_at&order[direction]=desc |
968 | 908 | |
969 | 909 | <a name="method-array-random"></a> |
970 | ||
971 | 910 | #### `Arr::random()` {.collection-method} |
972 | 911 | |
973 | 912 | `Arr::random` 方法从数组中随机返回一个值: | … | … |
988 | 927 | |
989 | 928 | // [2, 5] - (retrieved randomly) |
990 | 929 | |
991 | ||
992 | ||
993 | 930 | <a name="method-array-set"></a> |
994 | ||
995 | 931 | #### `Arr::set()` {.collection-method} |
996 | 932 | |
997 | 933 | `Arr::set` 方法使用「.」符号在多维数组中设置指定键的值: | … | … |
1005 | 941 | // ['products' => ['desk' => ['price' => 200]]] |
1006 | 942 | |
1007 | 943 | <a name="method-array-shuffle"></a> |
1008 | ||
1009 | 944 | #### `Arr::shuffle()` {.collection-method} |
1010 | 945 | |
1011 | 946 | `Arr::shuffle` 方法将数组中值进行随机排序: | … | … |
1017 | 952 | // [3, 2, 5, 1, 4] - (generated randomly) |
1018 | 953 | |
1019 | 954 | <a name="method-array-sort"></a> |
1020 | ||
1021 | 955 | #### `Arr::sort()` {.collection-method} |
1022 | 956 | |
1023 | 957 | `Arr::sort` 方法根据给定数组的值进行升序排序: | … | … |
1053 | 987 | */ |
1054 | 988 | |
1055 | 989 | <a name="method-array-sort-desc"></a> |
1056 | ||
1057 | 990 | #### `Arr::sortDesc()` {.collection-method} |
1058 | 991 | |
1059 | 992 | `Arr::sortDesc` 方法根据给定数组的值进行降序排序: | … | … |
1089 | 1022 | */ |
1090 | 1023 | |
1091 | 1024 | <a name="method-array-sort-recursive"></a> |
1092 | ||
1093 | 1025 | #### `Arr::sortRecursive()` {.collection-method} |
1094 | 1026 | |
1095 | 1027 | `Arr::sortRecursive` 方法对给定数组进行递归排序,使用 `sort` 方法对数字索引子数组进行按值升序排序,使用 `ksort` 方法对关联子数组进行按键升序排序: | … | … |
1115 | 1047 | |
1116 | 1048 | |
1117 | 1049 | <a name="method-array-to-css-classes"></a> |
1118 | ||
1119 | 1050 | #### `Arr::toCssClasses()` {.collection-method} |
1120 | 1051 | |
1121 | 1052 | `Arr::toCssClasses` 方法根据给定的条件编译并返回 CSS 类字符串。该方法接受一个类数组,其中数组键包含你希望添加的一个或多个 CSS Class,而值是一个布尔表达式。如果数组元素有一个数字键,它将始终包含在呈现的类列表中: | … | … |
1133 | 1064 | 'p-4 bg-red' |
1134 | 1065 | */ |
1135 | 1066 | |
1136 | Laravel 基于该方法实现 [Blade | |
1067 | Laravel 基于该方法实现 [Blade 组件里的条件合并类](/docs/laravel/10.x/blademd#conditionally-merge-classes) 以及 `@class` [Blade 指令](/docs/laravel/10.x/blademd#conditional-classes): | |
1137 | 1068 | |
1138 | 1069 | <a name="method-array-undot"></a> |
1139 | ||
1140 | 1070 | #### `Arr::undot()` {.collection-method} |
1141 | 1071 | |
1142 | 1072 | `Arr::undot` 方法使用「.」符号将一维数组扩展为多维数组: | … | … |
1169 | 1099 | // [1 => '200', 3 => '400'] |
1170 | 1100 | |
1171 | 1101 | <a name="method-array-where-not-null"></a> |
1172 | ||
1173 | 1102 | #### `Arr::whereNotNull()` {.collection-method} |
1174 | 1103 | |
1175 | 1104 | `Arr::whereNotNull` 方法将从给定数组中删除所有 `null` 值: | … | … |
1183 | 1112 | // [0 => 0] |
1184 | 1113 | |
1185 | 1114 | <a name="method-array-wrap"></a> |
1186 | ||
1187 | 1115 | #### `Arr::wrap()` {.collection-method} |
1188 | 1116 | |
1189 | 1117 | `Arr::wrap` 方法可以将给定值转换为一个数组,如果给定的值已经是一个数组,它将原样返回: | … | … |
1196 | 1124 | |
1197 | 1125 | // ['Laravel'] |
1198 | 1126 | |
1199 | ||
1200 | ||
1201 | 1127 | 如果给定值是 `null` ,将返回一个空数组: |
1202 | 1128 | |
1203 | 1129 | use Illuminate\Support\Arr; | … | … |
1207 | 1133 | // [] |
1208 | 1134 | |
1209 | 1135 | <a name="method-data-fill"></a> |
1210 | ||
1211 | 1136 | #### `data_fill()` {#collection-method} |
1212 | 1137 | |
1213 | 1138 | `data_fill` 函数使用「.」符号给多维数组或对象设置缺少的值: | … | … |
1243 | 1168 | */ |
1244 | 1169 | |
1245 | 1170 | <a name="method-data-get"></a> |
1246 | ||
1247 | 1171 | #### `data_get()` {#collection-method} |
1248 | 1172 | |
1249 | 1173 | `data_get` 函数使用 「.」 符号从多维数组或对象中根据指定键检索值 | … | … |
1272 | 1196 | // ['Desk 1', 'Desk 2']; |
1273 | 1197 | |
1274 | 1198 | <a name="method-data-set"></a> |
1275 | ||
1276 | 1199 | #### `data_set()` {#collection-method} |
1277 | 1200 | |
1278 | 1201 | `data_set` 函数使用「.」符号从多维数组或对象中根据指定键设置值: | … | … |
1303 | 1226 | ] |
1304 | 1227 | */ |
1305 | 1228 | |
1306 | ||
1307 | ||
1308 | 1229 | 通常情况下,已存在的值将会被覆盖。如果只是希望设置一个目前不存在的值,你可以增加一个 `false` 作为函数的第四个参数: |
1309 | 1230 | |
1310 | 1231 | $data = ['products' => ['desk' => ['price' => 100]]]; | … | … |
1314 | 1235 | // ['products' => ['desk' => ['price' => 100]]] |
1315 | 1236 | |
1316 | 1237 | <a name="method-head"></a> |
1317 | ||
1318 | 1238 | #### `head()` {#collection-method} |
1319 | 1239 | |
1320 | 1240 | `head` 函数将返回数组中的第一个值: | … | … |
1338 | 1258 | // 300 |
1339 | 1259 | |
1340 | 1260 | <a name="paths"></a> |
1341 | ||
1342 | 1261 | ## 路径 |
1343 | 1262 | |
1344 | 1263 | <a name="method-app-path"></a> |
1345 | ||
1346 | 1264 | #### `app_path()` {.collection-method} |
1347 | 1265 | |
1348 | 1266 | `app_path` 函数返回 `app` 目录的完整路径。你也可以使用 `app_path` 函数来生成应用目录下特定文件的完整路径: | … | … |
1352 | 1270 | $path = app_path('Http/Controllers/Controller.php'); |
1353 | 1271 | |
1354 | 1272 | <a name="method-base-path"></a> |
1355 | ||
1356 | 1273 | #### `base_path()` {#collection-method} |
1357 | 1274 | |
1358 | 1275 | `base_path` 函数返回项目根目录的完整路径。你也可以使用 `base_path` 函数生成项目根目录下特定文件的完整路径: | … | … |
1362 | 1279 | $path = base_path('vendor/bin'); |
1363 | 1280 | |
1364 | 1281 | <a name="method-config-path"></a> |
1365 | ||
1366 | 1282 | #### `config_path()` {#collection-method} |
1367 | 1283 | |
1368 | 1284 | `config_path` 函数返回项目配置目录 (config) 的完整路径。你也可以使用 `config_path` 函数来生成应用配置目录中的特定文件的完整路径: | … | … |
1372 | 1288 | $path = config_path('app.php'); |
1373 | 1289 | |
1374 | 1290 | <a name="method-database-path"></a> |
1375 | ||
1376 | ||
1377 | 1291 | #### `database_path()` {.collection-method} |
1378 | 1292 | |
1379 | 1293 | `database_path` 函数返回 `database` 目录的完整路径。你可以使用 `database_path` 函数来生成数据库目录下指定文件的完整路径: | … | … |
1383 | 1297 | $path = database_path('factories/UserFactory.php'); |
1384 | 1298 | |
1385 | 1299 | <a name="method-lang-path"></a> |
1386 | ||
1387 | 1300 | #### `lang_path()` {.collection-method} |
1388 | 1301 | |
1389 | 1302 | The `lang_path` 函数返回 `lang` 目录的完整路径。你可以使用 `lang_path` 函数来生成自定义语言目录下指定文件的完整路径: | … | … |
1396 | 1309 | > 默认情况下,Laravel 框架不包含 `lang` 目录。如果你想自定义 Laravel 的语言文件,可以通过 Artisan 命令 `lang:publish` 来发布它们。 |
1397 | 1310 | |
1398 | 1311 | <a name="method-mix"></a> |
1399 | ||
1400 | 1312 | #### `mix()` {.collection-method} |
1401 | 1313 | |
1402 | 1314 | `mix` 函数返回 [编译前端资源(Mix)的路径](/docs/laravel/10.x/mix),便于加载 css,js 等静态文件: | … | … |
1404 | 1316 | $path = mix('css/app.css'); |
1405 | 1317 | |
1406 | 1318 | <a name="method-public-path"></a> |
1407 | ||
1408 | 1319 | #### `public_path()` {.collection-method} |
1409 | 1320 | |
1410 | 1321 | `public_path` 函数返回 `public` 目录的完整路径。你可以使用 `public_path` 函数来生成`public` 目录下指定文件的完整路径: | … | … |
1414 | 1325 | $path = public_path('css/app.css'); |
1415 | 1326 | |
1416 | 1327 | <a name="method-resource-path"></a> |
1417 | ||
1418 | 1328 | #### `resource_path()` {.collection-method} |
1419 | 1329 | |
1420 | 1330 | `resource_path` 函数返回 `resource` 目录的完整路径。你可以使用 `resource_path` 函数来生成位于资源路径中指定文件的完整路径: | … | … |
1423 | 1333 | |
1424 | 1334 | $path = resource_path('sass/app.scss'); |
1425 | 1335 | |
1426 | ||
1427 | ||
1428 | 1336 | <a name="method-storage-path"></a> |
1429 | ||
1430 | 1337 | #### `storage_path()` |
1431 | 1338 | |
1432 | 1339 | `storage_path` 函数返回 `storage` 目录的完整路径。 你也可以用 `storage_path` 函数来生成位于资源路径中的特定文件路径 | … | … |
1604 | 1511 | // true |
1605 | 1512 | |
1606 | 1513 | <a name="method-str-contains-all"></a> |
1607 | ||
1608 | 1514 | #### `Str::containsAll()` |
1609 | 1515 | |
1610 | 1516 | `Str::containsAll` 方法用于判断指定字符串是否包含指定数组中的所有值: | … | … |
1616 | 1522 | // true |
1617 | 1523 | |
1618 | 1524 | <a name="method-ends-with"></a> |
1619 | ||
1620 | 1525 | #### `Str::endsWith()` |
1621 | 1526 | |
1622 | 1527 | `Str::endsWith` 方法用于判断指定字符串是否以另一指定字符串结尾: | … | … |
1640 | 1545 | // false |
1641 | 1546 | |
1642 | 1547 | <a name="method-excerpt"></a> |
1643 | ||
1644 | 1548 | #### `Str::excerpt()` |
1645 | 1549 | |
1646 | 1550 | `Str::excerpt` 方法提取字符串中给定短语匹配到的第一个片段: | … | … |
1654 | 1558 | // '...is my na...' |
1655 | 1559 | |
1656 | 1560 | `radius` 选项默认为 `100`,允许你定义应出现在截断字符串前后的字符数。 |
1657 | ||
1658 | ||
1659 | 1561 | |
1660 | 1562 | 此外,你可以使用`omission`选项来定义将附加到截断字符串的字符串: |
1661 | 1563 | … | … |
1685 | 1587 | // this/string/ |
1686 | 1588 | |
1687 | 1589 | <a name="method-str-headline"></a> |
1688 | ||
1689 | 1590 | #### `Str::headline()` {.collection-method} |
1690 | 1591 | |
1691 | 1592 | `Str::headline`方法会将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串,同时保证每个单词的首字母大写: | … | … |
1701 | 1602 | // 邮件通知发送 |
1702 | 1603 | |
1703 | 1604 | <a name="method-str-inline-markdown"></a> |
1704 | ||
1705 | 1605 | #### `Str::inlineMarkdown()` {.collection-method} |
1706 | 1606 | |
1707 | 1607 | `Str::inlineMarkdown`方法使用[通用标记](https://commonmark.thephpleague.com/)将 GitHub 风味 Markdown 转换为内联 HTML。然而,与`markdown`方法不同的是,它不会将所有生成的 HTML 都包装在块级元素中: | … | … |
1713 | 1613 | // <strong>Laravel</strong> |
1714 | 1614 | |
1715 | 1615 | <a name="method-str-is"></a> |
1716 | ||
1717 | 1616 | #### `Str::is()` {.collection-method} |
1718 | 1617 | |
1719 | 1618 | `Str::is`方法用来判断字符串是否与指定模式匹配。星号`*`可用于表示通配符: | … | … |
1729 | 1628 | // false |
1730 | 1629 | |
1731 | 1630 | <a name="method-str-is-ascii"></a> |
1732 | ||
1733 | 1631 | #### `Str::isAscii()` {.collection-method} |
1734 | 1632 | |
1735 | 1633 | `Str::isAscii`方法用于判断字符串是否是 7 位 ASCII: | … | … |
1744 | 1642 | |
1745 | 1643 | // false |
1746 | 1644 | |
1747 | ||
1748 | ||
1749 | 1645 | <a name="method-str-is-json"></a> |
1750 | ||
1751 | 1646 | #### `Str::isJson()` {.collection-method} |
1752 | 1647 | |
1753 | 1648 | `Str::isJson`方法确定给定的字符串是否是有效的 JSON: | … | … |
1767 | 1662 | // false |
1768 | 1663 | |
1769 | 1664 | <a name="method-str-is-ulid"></a> |
1770 | ||
1771 | 1665 | #### `Str::isUlid()` {.collection-method} |
1772 | 1666 | |
1773 | 1667 | `Str::isUlid`方法用于判断指定字符串是否是有效的 ULID: | … | … |
1783 | 1677 | // false |
1784 | 1678 | |
1785 | 1679 | <a name="method-str-is-uuid"></a> |
1786 | ||
1787 | 1680 | #### `Str::isUuid()` {.collection-method} |
1788 | 1681 | |
1789 | 1682 | `Str::isUuid`方法用于判断指定字符串是否是有效的 UUID: | … | … |
1799 | 1692 | // false |
1800 | 1693 | |
1801 | 1694 | <a name="method-kebab-case"></a> |
1802 | ||
1803 | 1695 | #### `Str::kebab()` {.collection-method} |
1804 | 1696 | |
1805 | 1697 | `Str::kebab`方法将字符串转换为`烤串式( kebab-case )`表示方法: | … | … |
1811 | 1703 | // foo-bar |
1812 | 1704 | |
1813 | 1705 | <a name="method-str-lcfirst"></a> |
1814 | ||
1815 | 1706 | #### `Str::lcfirst()` {.collection-method} |
1816 | 1707 | |
1817 | 1708 | `Str::lcfirst`方法返回第一个小写字符的给定字符串: | … | … |
1823 | 1714 | // foo Bar |
1824 | 1715 | |
1825 | 1716 | <a name="method-str-length"></a> |
1826 | ||
1827 | 1717 | #### `Str::length()` {.collection-method} |
1828 | 1718 | |
1829 | 1719 | `Str::length`方法返回指定字符串的长度: | … | … |
1835 | 1725 | // 7 |
1836 | 1726 | |
1837 | 1727 | <a name="method-str-limit"></a> |
1838 | ||
1839 | 1728 | #### `Str::limit()` {.collection-method} |
1840 | 1729 | |
1841 | 1730 | `Str::limit`方法将字符串以指定长度进行截断: | … | … |
1846 | 1735 | |
1847 | 1736 | // 敏捷的棕色狐狸... |
1848 | 1737 | |
1849 | ||
1850 | ||
1851 | 1738 | 你也可通过第三个参数来改变追加到末尾的字符串: |
1852 | 1739 | |
1853 | 1740 | use Illuminate\Support\Str; | … | … |
1857 | 1744 | // 敏捷的棕色狐狸 (...) |
1858 | 1745 | |
1859 | 1746 | <a name="method-str-lower"></a> |
1860 | ||
1861 | 1747 | #### `Str::lower()` {.collection-method} |
1862 | 1748 | |
1863 | 1749 | `Str::lower`方法用于将字符串转换为小写: | … | … |
1869 | 1755 | // laravel |
1870 | 1756 | |
1871 | 1757 | <a name="method-str-markdown"></a> |
1872 | ||
1873 | 1758 | #### `Str::markdown()` {.collection-method} |
1874 | 1759 | |
1875 | `Str::markdown`方法将 GitHub 风格的 Markdown 转换为 HTML 使用 | |
1760 | `Str::markdown`方法将 GitHub 风格的 Markdown 转换为 HTML 使用 [通用标记](https://commonmark.thephpleague.com/): | |
1876 | 1761 | |
1877 | 1762 | use Illuminate\Support\Str; |
1878 | 1763 | … | … |
1887 | 1772 | // <h1>Taylor Otwell</h1> |
1888 | 1773 | |
1889 | 1774 | <a name="method-str-mask"></a> |
1890 | ||
1891 | 1775 | #### `Str::mask()` {.collection-method} |
1892 | 1776 | |
1893 | 1777 | `Str::mask`方法会使用重复的字符掩盖字符串的一部分,并可用于混淆字符串段,例如电子邮件地址和电话号码: | … | … |
1905 | 1789 | // tay***@example.com |
1906 | 1790 | |
1907 | 1791 | <a name="method-str-ordered-uuid"></a> |
1908 | ||
1909 | 1792 | #### `Str::orderedUuid()` {.collection-method} |
1910 | 1793 | |
1911 | 1794 | `Str::orderedUuid`方法用于生成一个「时间戳优先」的 UUID ,它可作为数据库索引列的有效值。使用此方法生成的每个 UUID 将排在之前使用该方法生成的 UUID 后面: | … | … |
1914 | 1797 | |
1915 | 1798 | return (string) Str::orderedUuid(); |
1916 | 1799 | |
1917 | ||
1918 | ||
1919 | 1800 | <a name="method-str-padboth"></a> |
1920 | ||
1921 | 1801 | #### `Str::padBoth()` {.collection-method} |
1922 | 1802 | |
1923 | 1803 | `Str::padBoth`方法包装了 PHP 的`str_pad 方法`,在指定字符串的两侧填充上另一字符串: | … | … |
1933 | 1813 | // ' James ' |
1934 | 1814 | |
1935 | 1815 | <a name="method-str-padleft"></a> |
1936 | ||
1937 | 1816 | #### `Str::padLeft()` {.collection-method} |
1938 | 1817 | |
1939 | 1818 | `Str::padLeft`方法包装了 PHP 的`str_pad`方法,在指定字符串的左侧填充上另一字符串: | … | … |
1949 | 1828 | // ' James' |
1950 | 1829 | |
1951 | 1830 | <a name="method-str-padright"></a> |
1952 | ||
1953 | 1831 | #### `Str::padRight()` {.collection-method} |
1954 | 1832 | |
1955 | 1833 | `Str::padRight`方法包装了 PHP 的`str_pad`方法,在指定字符串的右侧填充上另一字符串: | … | … |
1965 | 1843 | // 'James ' |
1966 | 1844 | |
1967 | 1845 | <a name="method-str-password"></a> |
1968 | ||
1969 | 1846 | #### `Str::password()` {.collection-method} |
1970 | 1847 | |
1971 | 1848 | `Str::password`方法可用于生成给定长度的安全随机密码。密码由字母、数字、符号和空格组成。默认情况下,密码长度为32位: | … | … |
1981 | 1858 | // 'qwuar>#V|i]N' |
1982 | 1859 | |
1983 | 1860 | <a name="method-str-plural"></a> |
1984 | ||
1985 | 1861 | #### `Str::plural()` {.collection-method} |
1986 | 1862 | |
1987 | `Str::plural`方法将单数形式的字符串转换为复数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization | |
1863 | `Str::plural`方法将单数形式的字符串转换为复数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localizationmd#pluralization-language): | |
1988 | 1864 | |
1989 | 1865 | use Illuminate\Support\Str; |
1990 | 1866 | … | … |
1996 | 1872 | |
1997 | 1873 | // children |
1998 | 1874 | |
1999 | ||
2000 | ||
2001 | 1875 | 你可以提供一个整数作为方法的第二个参数来检索字符串的单数或复数形式: |
2002 | 1876 | |
2003 | 1877 | use Illuminate\Support\Str; | … | … |
2011 | 1885 | // child |
2012 | 1886 | |
2013 | 1887 | <a name="method-str-plural-studly"></a> |
2014 | ||
2015 | 1888 | #### `Str::pluralStudly()` {.collection-method} |
2016 | 1889 | |
2017 | 1890 | `Str::pluralStudly`方法将以驼峰格式的单数字符串转化为其复数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization#pluralization-language): | … | … |
2039 | 1912 | // VerifiedHuman |
2040 | 1913 | |
2041 | 1914 | <a name="method-str-random"></a> |
2042 | ||
2043 | 1915 | #### `Str::random()` {.collection-method} |
2044 | 1916 | |
2045 | 1917 | `Str::random` 方法用于生成指定长度的随机字符串。这个方法使用了PHP的 `random_bytes` 函数: | … | … |
2049 | 1921 | $random = Str::random(40); |
2050 | 1922 | |
2051 | 1923 | <a name="method-str-remove"></a> |
2052 | ||
2053 | 1924 | #### `Str::remove()` {.collection-method} |
2054 | 1925 | |
2055 | 1926 | `Str::remove` 方法从字符串中删除给定值或给定数组内的所有值: | … | … |
2065 | 1936 | 你还可以将`false`作为第三个参数传递给`remove`方法以在删除字符串时忽略大小写。 |
2066 | 1937 | |
2067 | 1938 | <a name="method-str-replace"></a> |
2068 | ||
2069 | 1939 | #### `Str::replace()` {.collection-method} |
2070 | 1940 | |
2071 | 1941 | `Str::replace` 方法用于替换字符串中的给定字符串: |
2072 | 1942 | |
2073 | 1943 | use Illuminate\Support\Str; |
2074 | 1944 | |
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 | |
2082 | 1950 | |
2083 | 1951 | <a name="method-str-replace-array"></a> |
2084 | ||
2085 | 1952 | #### `Str::replaceArray()` {.collection-method} |
2086 | 1953 | |
2087 | 1954 | `Str::replaceArray` 方法使用数组有序的替换字符串中的特定字符: | … | … |
2095 | 1962 | // 该活动将在 8:30 至 9:00 举行 |
2096 | 1963 | |
2097 | 1964 | <a name="method-str-replace-first"></a> |
2098 | ||
2099 | 1965 | #### `Str::replaceFirst()` {.collection-method} |
2100 | 1966 | |
2101 | 1967 | `Str::replaceFirst` 方法替换字符串中给定值的第一个匹配项: | … | … |
2107 | 1973 | // a quick brown fox jumps over the lazy dog |
2108 | 1974 | |
2109 | 1975 | <a name="method-str-replace-last"></a> |
2110 | ||
2111 | 1976 | #### `Str::replaceLast()` {.collection-method} |
2112 | 1977 | |
2113 | 1978 | `Str::replaceLast` 方法替换字符串中最后一次出现的给定值: | … | … |
2118 | 1983 | |
2119 | 1984 | // the quick brown fox jumps over a lazy dog |
2120 | 1985 | |
2121 | ||
2122 | 1986 | <a name="method-str-reverse"></a> |
2123 | ||
2124 | 1987 | #### `Str::reverse()` {.collection-method} |
2125 | 1988 | |
2126 | 1989 | `Str::reverse` 方法用于反转给定的字符串: | … | … |
2132 | 1995 | // dlroW olleH |
2133 | 1996 | |
2134 | 1997 | <a name="method-str-singular"></a> |
2135 | ||
2136 | 1998 | #### `Str::singular()` {.collection-method} |
2137 | 1999 | |
2138 | `Str::singular` 方法将字符串转换为单数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localization | |
2000 | `Str::singular` 方法将字符串转换为单数形式。此方法支持 [Laravel 复数形式所支持的任何语言](/docs/laravel/10.x/localizationmd#pluralization-language): | |
2139 | 2001 | |
2140 | 2002 | use Illuminate\Support\Str; |
2141 | 2003 | … | … |
2148 | 2010 | // child |
2149 | 2011 | |
2150 | 2012 | <a name="method-str-slug"></a> |
2151 | ||
2152 | 2013 | #### `Str::slug()` {.collection-method} |
2153 | 2014 | |
2154 | 2015 | `Str::slug` 方法将给定的字符串生成一个 URL 友好的「slug」: |
2155 | 2016 | |
2156 | 2017 | use Illuminate\Support\Str; |
2157 | 2018 | |
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 | |
2163 | 2022 | |
2164 | 2023 | <a name="method-snake-case"></a> |
2165 | ||
2166 | 2024 | #### `Str::snake()` {.collection-method} |
2167 | 2025 | |
2168 | 2026 | `Str::snake` 方法是将驼峰的函数名或者字符串转换成 `_` 命名的函数或者字符串,例如 `snakeCase` 转换成 `snake_case`: | … | … |
2178 | 2036 | // foo-bar |
2179 | 2037 | |
2180 | 2038 | <a name="method-str-squish"></a> |
2181 | ||
2182 | 2039 | #### `Str::squish()` {.collection-method} |
2183 | 2040 | |
2184 | 2041 | `Str::squish`方法删除字符串中所有多余的空白,包括单词之间多余的空白: | … | … |
2190 | 2047 | // laravel framework |
2191 | 2048 | |
2192 | 2049 | <a name="method-str-start"></a> |
2193 | ||
2194 | 2050 | #### `Str::start()` {.collection-method} |
2195 | 2051 | |
2196 | 2052 | `Str::start`方法是将给定的值添加到字符串的开始位置,例如: | … | … |
2206 | 2062 | // /this/string |
2207 | 2063 | |
2208 | 2064 | <a name="method-starts-with"></a> |
2209 | ||
2210 | 2065 | #### `Str::startsWith()` {.collection-method} |
2211 | 2066 | |
2212 | 2067 | `Str::startsWith`方法用来判断给定的字符串是否为给定值的开头: | … | … |
2224 | 2079 | // true |
2225 | 2080 | |
2226 | 2081 | <a name="method-studly-case"></a> |
2227 | ||
2228 | 2082 | #### `Str::studly()` {.collection-method} |
2229 | 2083 | |
2230 | 2084 | `Str::studly`方法将给定的字符串转换为`驼峰命名`的字符串: | … | … |
2236 | 2090 | // FooBar |
2237 | 2091 | |
2238 | 2092 | <a name="method-str-substr"></a> |
2239 | ||
2240 | 2093 | #### `Str::substr()` {.collection-method} |
2241 | 2094 | |
2242 | 2095 | `Str::substr`方法返回由 start 和 length 参数指定的字符串部分: | … | … |
2247 | 2100 | |
2248 | 2101 | // Laravel |
2249 | 2102 | |
2250 | ||
2251 | ||
2252 | 2103 | <a name="method-str-substrcount"></a> |
2253 | ||
2254 | 2104 | #### `Str::substrCount()` {.collection-method} |
2255 | 2105 | |
2256 | 2106 | `Str::substrCount` 方法返回给定字符串中给定值的出现次数: | … | … |
2262 | 2112 | // 2 |
2263 | 2113 | |
2264 | 2114 | <a name="method-str-substrreplace"></a> |
2265 | ||
2266 | 2115 | #### `Str::substrReplace()` {.collection-method} |
2267 | 2116 | |
2268 | 2117 | `Str::substrReplace` 方法替换字符串一部分中的文本,从第三个参数指定的位置开始,替换第四个参数指定的字符数。 当「0」传递给方法的第四个参数将在指定位置插入字符串,而不是替换字符串中的任何现有字符: | … | … |
2276 | 2125 | // 13:00 |
2277 | 2126 | |
2278 | 2127 | <a name="method-str-swap"></a> |
2279 | ||
2280 | 2128 | #### `Str::swap()` {.collection-method} |
2281 | 2129 | |
2282 | 2130 | `Str::swap` 方法使用 PHP 的 `strtr` 函数替换给定字符串中的多个值: | … | … |
2288 | 2136 | 'great' => 'fantastic', |
2289 | 2137 | ], 'Tacos are great!'); |
2290 | 2138 | |
2291 | // Burritos are fantastic | |
2139 | // Burritos are fantastic! | |
2292 | 2140 | |
2293 | 2141 | <a name="method-title-case"></a> |
2294 | ||
2295 | 2142 | #### `Str::title()` {.collection-method} |
2296 | 2143 | |
2297 | 2144 | `Str::title` 方法将给定的字符串转换为 `Title Case`: | … | … |
2303 | 2150 | // A Nice Title Uses The Correct Case |
2304 | 2151 | |
2305 | 2152 | <a name="method-str-to-html-string"></a> |
2306 | ||
2307 | 2153 | #### `Str::toHtmlString()` {.collection-method} |
2308 | 2154 | |
2309 | 2155 | `Str::toHtmlString` 方法将字符串实例转换为 `Illuminate\Support\HtmlString` 的实例,它可以显示在 Blade 模板中: | … | … |
2313 | 2159 | $htmlString = Str::of('Nuno Maduro')->toHtmlString(); |
2314 | 2160 | |
2315 | 2161 | <a name="method-str-ucfirst"></a> |
2316 | ||
2317 | 2162 | #### `Str::ucfirst()` {.collection-method} |
2318 | 2163 | |
2319 | 2164 | `Str::ucfirst` 方法返回第一个字符大写的给定字符串: | … | … |
2324 | 2169 | |
2325 | 2170 | // Foo bar |
2326 | 2171 | |
2327 | ||
2328 | ||
2329 | 2172 | <a name="method-str-ucsplit"></a> |
2330 | ||
2331 | 2173 | #### `Str::ucsplit()` {.collection-method} |
2332 | 2174 | |
2333 | 2175 | `Str::ucsplit` 方法将给定的字符串按大写字符拆分为数组: | … | … |
2339 | 2181 | // [0 => 'Foo', 1 => 'Bar'] |
2340 | 2182 | |
2341 | 2183 | <a name="method-str-upper"></a> |
2342 | ||
2343 | 2184 | #### `Str::upper()` {.collection-method} |
2344 | 2185 | |
2345 | 2186 | `Str::upper` 方法将给定的字符串转换为大写: | … | … |
2351 | 2192 | // LARAVEL |
2352 | 2193 | |
2353 | 2194 | <a name="method-str-ulid"></a> |
2354 | ||
2355 | 2195 | #### `Str::ulid()` {.collection-method} |
2356 | 2196 | |
2357 | 2197 | `Str::ulid` 方法生成一个 ULID: | … | … |
2363 | 2203 | // 01gd6r360bp37zj17nxb55yv40 |
2364 | 2204 | |
2365 | 2205 | <a name="method-str-uuid"></a> |
2366 | ||
2367 | 2206 | #### `Str::uuid()` {.collection-method} |
2368 | 2207 | |
2369 | 2208 | `Str::uuid` 方法生成一个 UUID(版本 4): | … | … |
2373 | 2212 | return (string) Str::uuid(); |
2374 | 2213 | |
2375 | 2214 | <a name="method-str-word-count"></a> |
2376 | ||
2377 | 2215 | #### `Str::wordCount()` {.collection-method} |
2378 | 2216 | |
2379 | 2217 | `Str::wordCount` 方法返回字符串包含的单词数 | … | … |
2385 | 2223 | ``` |
2386 | 2224 | |
2387 | 2225 | <a name="method-str-words"></a> |
2388 | ||
2389 | 2226 | #### `Str::words()` {.collection-method} |
2390 | 2227 | |
2391 | 2228 | `Str::words` 方法限制字符串中的单词数。 可以通过其第三个参数将附加字符串传递给此方法,以指定应将这个字符串附加到截断后的字符串末尾: | … | … |
2397 | 2234 | // Perfectly balanced, as >>> |
2398 | 2235 | |
2399 | 2236 | <a name="method-str"></a> |
2400 | ||
2401 | 2237 | #### `str()` {.collection-method} |
2402 | 2238 | |
2403 | 2239 | `str` 函数返回给定字符串的新 `Illuminate\Support\Stringable` 实例。 此函数等效于 `Str::of` 方法: | … | … |
2412 | 2248 | |
2413 | 2249 | // 'foo_bar' |
2414 | 2250 | |
2415 | ||
2416 | ||
2417 | 2251 | <a name="method-trans"></a> |
2418 | ||
2419 | 2252 | #### `trans()` {.collection-method} |
2420 | 2253 | |
2421 | `trans` 函数使用你的 [语言文件](/docs/laravel/10.x/localization | |
2254 | `trans` 函数使用你的 [语言文件](/docs/laravel/10.x/localizationmd) 翻译给定的翻译键: | |
2422 | 2255 | |
2423 | 2256 | ``` |
2424 | 2257 | echo trans('messages.welcome'); | … | … |
2439 | 2272 | 如果指定的翻译键不存在,`trans_choice` 函数将返回给定的键。 因此,使用上面的示例,如果翻译键不存在,`trans_choice` 函数将返回 `messages.notifications`。 |
2440 | 2273 | |
2441 | 2274 | <a name="fluent-strings"></a> |
2442 | ||
2443 | 2275 | ## 字符流处理 |
2444 | 2276 | |
2445 | 2277 | Fluent strings 提供了一个更流畅的、面向对象的接口来处理字符串值,与传统的字符串操作相比,允许你使用更易读的语法将多个字符串操作链接在一起。 |
2446 | 2278 | |
2447 | 2279 | <a name="method-fluent-str-after"></a> |
2448 | ||
2449 | 2280 | #### `after` {.collection-method} |
2450 | 2281 | |
2451 | 2282 | `after` 方法返回字符串中给定值之后的所有内容。 如果字符串中不存在该值,则将返回整个字符串: | … | … |
2457 | 2288 | // ' my name' |
2458 | 2289 | |
2459 | 2290 | <a name="method-fluent-str-after-last"></a> |
2460 | ||
2461 | 2291 | #### `afterLast` {.collection-method} |
2462 | 2292 | |
2463 | 2293 | `afterLast` 方法返回字符串中最后一次出现给定值之后的所有内容。 如果字符串中不存在该值,则将返回整个字符串 | … | … |
2468 | 2298 | |
2469 | 2299 | // 'Controller' |
2470 | 2300 | |
2471 | ||
2472 | ||
2473 | 2301 | <a name="method-fluent-str-append"></a> |
2474 | ||
2475 | 2302 | #### `append` {.collection-method} |
2476 | 2303 | |
2477 | 2304 | `append` 方法将给定的值附加到字符串: | … | … |
2483 | 2310 | // 'Taylor Otwell' |
2484 | 2311 | |
2485 | 2312 | <a name="method-fluent-str-ascii"></a> |
2486 | ||
2487 | 2313 | #### `ascii` {.collection-method} |
2488 | 2314 | |
2489 | 2315 | `ascii` 方法将尝试将字符串音译为 ASCII 值: | … | … |
2495 | 2321 | // 'u' |
2496 | 2322 | |
2497 | 2323 | <a name="method-fluent-str-basename"></a> |
2498 | ||
2499 | 2324 | #### `basename` {.collection-method} |
2500 | 2325 | |
2501 | 2326 | `basename` 方法将返回给定字符串的结尾名称部分: | … | … |
2515 | 2340 | // 'baz' |
2516 | 2341 | |
2517 | 2342 | <a name="method-fluent-str-before"></a> |
2518 | ||
2519 | 2343 | #### `before` {.collection-method} |
2520 | 2344 | |
2521 | 2345 | `before` 方法返回字符串中给定值之前的所有内容: | … | … |
2527 | 2351 | // 'This is ' |
2528 | 2352 | |
2529 | 2353 | <a name="method-fluent-str-before-last"></a> |
2530 | ||
2531 | 2354 | #### `beforeLast` {.collection-method} |
2532 | 2355 | |
2533 | 2356 | `beforeLast` 方法返回字符串中最后一次出现给定值之前的所有内容: | … | … |
2539 | 2362 | // 'This ' |
2540 | 2363 | |
2541 | 2364 | <a name="method-fluent-str-between"></a> |
2542 | ||
2543 | 2365 | #### `between` {.collection-method} |
2544 | 2366 | |
2545 | 2367 | `between` 方法返回两个值之间的字符串部分: | … | … |
2551 | 2373 | // ' is my ' |
2552 | 2374 | |
2553 | 2375 | <a name="method-fluent-str-between-first"></a> |
2554 | ||
2555 | 2376 | #### `betweenFirst` {.collection-method} |
2556 | 2377 | |
2557 | 2378 | `betweenFirst` 方法返回两个值之间字符串的最小可能部分: | … | … |
2562 | 2383 | |
2563 | 2384 | // 'a' |
2564 | 2385 | |
2565 | ||
2566 | ||
2567 | 2386 | <a name="method-fluent-str-camel"></a> |
2568 | ||
2569 | 2387 | #### `camel` {.collection-method} |
2570 | 2388 | |
2571 | 2389 | `camel` 方法将给定的字符串转换为 `camelCase`: | … | … |
2577 | 2395 | // fooBar |
2578 | 2396 | |
2579 | 2397 | <a name="method-fluent-str-class-basename"></a> |
2580 | ||
2581 | 2398 | #### `classBasename` {.collection-method} |
2582 | 2399 | |
2583 | 2400 | `classBasename` 方法返回给定类的类名,删除了类的命名空间: | … | … |
2589 | 2406 | // Baz |
2590 | 2407 | |
2591 | 2408 | <a name="method-fluent-str-contains"></a> |
2592 | ||
2593 | 2409 | #### `contains` {.collection-method} |
2594 | 2410 | |
2595 | 2411 | `contains` 方法确定给定的字符串是否包含给定的值。 此方法区分大小写: | … | … |
2609 | 2425 | // true |
2610 | 2426 | |
2611 | 2427 | <a name="method-fluent-str-contains-all"></a> |
2612 | ||
2613 | 2428 | #### `containsAll` {.collection-method} |
2614 | 2429 | |
2615 | 2430 | `containsAll` 方法确定给定字符串是否包含给定数组中的所有值: | … | … |
2621 | 2436 | // true |
2622 | 2437 | |
2623 | 2438 | <a name="method-fluent-str-dirname"></a> |
2624 | ||
2625 | 2439 | #### `dirname` {.collection-method} |
2626 | 2440 | |
2627 | 2441 | `dirname` 方法返回给定字符串的父目录部分: | … | … |
2641 | 2455 | // '/foo' |
2642 | 2456 | |
2643 | 2457 | <a name="method-fluent-str-excerpt"></a> |
2644 | ||
2645 | 2458 | #### `excerpt` {.collection-method} |
2646 | 2459 | |
2647 | 2460 | `excerpt` 方法从字符串中提取与该字符串中短语的第一个实例匹配的摘录: | … | … |
2653 | 2466 | ]); |
2654 | 2467 | |
2655 | 2468 | // '...is my na...' |
2656 | ||
2657 | ||
2658 | 2469 | |
2659 | 2470 | `radius` 选项默认为 `100`,允许你定义应出现在截断字符串每一侧的字符数。 |
2660 | 2471 | … | … |
2670 | 2481 | // '(...) my name' |
2671 | 2482 | |
2672 | 2483 | <a name="method-fluent-str-ends-with"></a> |
2673 | ||
2674 | 2484 | #### `endsWith` {.collection-method} |
2675 | 2485 | |
2676 | 2486 | `endsWith` 方法确定给定字符串是否以给定值结尾: | … | … |
2694 | 2504 | // false |
2695 | 2505 | |
2696 | 2506 | <a name="method-fluent-str-exactly"></a> |
2697 | ||
2698 | 2507 | #### `exactly` {.collection-method} |
2699 | 2508 | |
2700 | 2509 | `exactly` 方法确定给定的字符串是否与另一个字符串完全匹配: | … | … |
2706 | 2515 | // true |
2707 | 2516 | |
2708 | 2517 | <a name="method-fluent-str-explode"></a> |
2709 | ||
2710 | 2518 | #### `explode` {.collection-method} |
2711 | 2519 | |
2712 | 2520 | `explode` 方法按给定的分隔符拆分字符串并返回包含拆分字符串的每个部分的集合: | … | … |
2718 | 2526 | // collect(['foo', 'bar', 'baz']) |
2719 | 2527 | |
2720 | 2528 | <a name="method-fluent-str-finish"></a> |
2721 | ||
2722 | 2529 | #### `finish` {.collection-method} |
2723 | 2530 | |
2724 | 2531 | `finish` 方法将给定值的单个实例添加到字符串中(如果它尚未以该值结尾): | … | … |
2732 | 2539 | |
2733 | 2540 | // this/string/ |
2734 | 2541 | |
2735 | ||
2736 | ||
2737 | 2542 | <a name="method-fluent-str-headline"></a> |
2738 | ||
2739 | 2543 | #### `headline` {.collection-method} |
2740 | 2544 | |
2741 | 2545 | `headline` 方法会将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串,每个单词的首字母大写: | … | … |
2751 | 2555 | // Email Notification Sent |
2752 | 2556 | |
2753 | 2557 | <a name="method-fluent-str-inline-markdown"></a> |
2754 | ||
2755 | 2558 | #### `inlineMarkdown` {.collection-method} |
2756 | 2559 | |
2757 | 2560 | `inlineMarkdown` 方法使用 [CommonMark](https://commonmark.thephpleague.com/) 将 GitHub 风格的 Markdown 转换为内联 HTML。 但是,与 `markdown` 方法不同,它不会将所有生成的 HTML 包装在块级元素中: | … | … |
2763 | 2566 | // <strong>Laravel</strong> |
2764 | 2567 | |
2765 | 2568 | <a name="method-fluent-str-is"></a> |
2766 | ||
2767 | 2569 | #### `is` {.collection-method} |
2768 | 2570 | |
2769 | 2571 | `is` 方法确定给定字符串是否与给定模式匹配。 星号可用作通配符值 | … | … |
2779 | 2581 | // false |
2780 | 2582 | |
2781 | 2583 | <a name="method-fluent-str-is-ascii"></a> |
2782 | ||
2783 | 2584 | #### `isAscii` {.collection-method} |
2784 | 2585 | |
2785 | 2586 | `isAscii` 方法确定给定字符串是否为 ASCII 字符串: | … | … |
2795 | 2596 | // false |
2796 | 2597 | |
2797 | 2598 | <a name="method-fluent-str-is-empty"></a> |
2798 | ||
2799 | 2599 | #### `isEmpty` {.collection-method} |
2800 | 2600 | |
2801 | 2601 | `isEmpty` 方法确定给定的字符串是否为空: | … | … |
2811 | 2611 | // false |
2812 | 2612 | |
2813 | 2613 | <a name="method-fluent-str-is-not-empty"></a> |
2814 | ||
2815 | 2614 | #### `isNotEmpty` {.collection-method} |
2816 | 2615 | |
2817 | 2616 | `isNotEmpty` 方法确定给定的字符串是否不为空: | … | … |
2826 | 2625 | |
2827 | 2626 | // true |
2828 | 2627 | |
2829 | ||
2830 | ||
2831 | 2628 | <a name="method-fluent-str-is-json"></a> |
2832 | ||
2833 | 2629 | #### `isJson` {.collection-method} |
2834 | 2630 | |
2835 | 2631 | `isJson` 方法确定给定的字符串是否是有效的 JSON: | … | … |
2849 | 2645 | // false |
2850 | 2646 | |
2851 | 2647 | <a name="method-fluent-str-is-ulid"></a> |
2852 | ||
2853 | 2648 | #### `isUlid` {.collection-method} |
2854 | 2649 | |
2855 | 2650 | `isUlid` 方法确定给定的字符串是否一个 ULID: | … | … |
2865 | 2660 | // false |
2866 | 2661 | |
2867 | 2662 | <a name="method-fluent-str-is-uuid"></a> |
2868 | ||
2869 | 2663 | #### `isUuid` {.collection-method} |
2870 | 2664 | |
2871 | 2665 | `isUuid` 方法确定给定的字符串是否是一个 UUID: | … | … |
2881 | 2675 | // false |
2882 | 2676 | |
2883 | 2677 | <a name="method-fluent-str-kebab"></a> |
2884 | ||
2885 | 2678 | #### `kebab` {.collection-method} |
2886 | 2679 | |
2887 | 2680 | `kebab` 方法转变给定的字符串为 `kebab-case`: | … | … |
2904 | 2697 | |
2905 | 2698 | // foo Bar |
2906 | 2699 | |
2907 | ||
2908 | 2700 | <a name="method-fluent-str-length"></a> |
2909 | 2701 | |
2910 | 2702 | #### `length` {.collection-method} | … | … |
2918 | 2710 | // 7 |
2919 | 2711 | |
2920 | 2712 | <a name="method-fluent-str-limit"></a> |
2921 | ||
2922 | 2713 | #### `limit` {.collection-method} |
2923 | 2714 | |
2924 | 2715 | `limit` 方法将给定的字符串截断为指定的长度: | … | … |
2929 | 2720 | |
2930 | 2721 | // The quick brown fox... |
2931 | 2722 | |
2932 | ||
2933 | ||
2934 | 2723 | 你也可以通过第二个参数来改变追加到末尾的字符串: |
2935 | 2724 | |
2936 | 2725 | use Illuminate\Support\Str; | … | … |
2940 | 2729 | // The quick brown fox (...) |
2941 | 2730 | |
2942 | 2731 | <a name="method-fluent-str-lower"></a> |
2943 | ||
2944 | 2732 | #### `lower` |
2945 | 2733 | |
2946 | 2734 | `lower` 方法将指定字符串转换为小写: | … | … |
2952 | 2740 | // 'laravel' |
2953 | 2741 | |
2954 | 2742 | <a name="method-fluent-str-ltrim"></a> |
2955 | ||
2956 | 2743 | #### `ltrim` |
2957 | 2744 | |
2958 | 2745 | `ltrim` 方法移除字符串左端指定的字符: | … | … |
2970 | 2757 | <a name="method-fluent-str-markdown"></a> |
2971 | 2758 | |
2972 | 2759 | #### `markdown` {.collection-method} |
2973 | ||
2974 | 2760 | `markdown` 方法将 Github 风格的 Markdown 转换为 HTML: |
2975 | 2761 | |
2976 | 2762 | use Illuminate\Support\Str; | … | … |
2986 | 2772 | // <h1>Taylor Otwell</h1> |
2987 | 2773 | |
2988 | 2774 | <a name="method-fluent-str-mask"></a> |
2989 | ||
2990 | 2775 | #### `mask` |
2991 | 2776 | |
2992 | 2777 | `mask` 方法用重复字符掩盖字符串的一部分,可用于模糊处理字符串的某些段,例如电子邮件地址和电话号码: | … | … |
3007 | 2792 | |
3008 | 2793 | // tayl**********.com |
3009 | 2794 | |
3010 | ||
3011 | ||
3012 | 2795 | <a name="method-fluent-str-match"></a> |
3013 | ||
3014 | 2796 | #### `match` |
3015 | 2797 | |
3016 | 2798 | `match` 方法将会返回字符串中和指定正则表达式匹配的部分: | … | … |
3026 | 2808 | // 'bar' |
3027 | 2809 | |
3028 | 2810 | <a name="method-fluent-str-match-all"></a> |
3029 | ||
3030 | 2811 | #### `matchAll` |
3031 | 2812 | |
3032 | 2813 | `matchAll` 方法将会返回一个集合,该集合包含了字符串中与指定正则表达式匹配的部分 | … | … |
3048 | 2829 | 如果没有找到任何匹配项,则返回空集合。 |
3049 | 2830 | |
3050 | 2831 | <a name="method-fluent-str-is-match"></a> |
3051 | ||
3052 | 2832 | #### `isMatch` |
3053 | 2833 | |
3054 | 2834 | `isMatch` 方法用于判断给定的字符串是否与正则表达式匹配: | … | … |
3064 | 2844 | // false |
3065 | 2845 | |
3066 | 2846 | <a name="method-fluent-str-new-line"></a> |
3067 | ||
3068 | 2847 | #### `newLine` |
3069 | 2848 | |
3070 | 2849 | `newLine` 方法将给字符串追加换行的字符: | … | … |
3077 | 2856 | // Framework' |
3078 | 2857 | |
3079 | 2858 | <a name="method-fluent-str-padboth"></a> |
3080 | ||
3081 | 2859 | #### `padBoth` |
3082 | 2860 | |
3083 | 2861 | `padBoth` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的两侧填充上另一字符串,直至该字符串到达指定的长度: | … | … |
3092 | 2870 | |
3093 | 2871 | // ' James ' |
3094 | 2872 | |
3095 | ||
3096 | ||
3097 | 2873 | <a name="method-fluent-str-padleft"></a> |
3098 | ||
3099 | 2874 | #### `padLeft` |
3100 | 2875 | |
3101 | 2876 | The `padLeft` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的左侧填充上另一字符串,直至该字符串到达指定的长度: | … | … |
3111 | 2886 | // ' James' |
3112 | 2887 | |
3113 | 2888 | <a name="method-fluent-str-padright"></a> |
3114 | ||
3115 | 2889 | #### `padRight` |
3116 | 2890 | |
3117 | 2891 | `padRight` 方法包装了 PHP 的 `str_pad` 函数,在指定字符串的右侧填充上另一字符串,直至该字符串到达指定的长度: | … | … |
3127 | 2901 | // 'James ' |
3128 | 2902 | |
3129 | 2903 | <a name="method-fluent-str-pipe"></a> |
3130 | ||
3131 | 2904 | #### `pipe` |
3132 | ||
3133 | 2905 | |
3134 | 2906 | `pipe` 方法将把字符串的当前值传递给指定的函数来转换字符串: |
3135 | 2907 | … | … |
3147 | 2919 | // 'bar' |
3148 | 2920 | |
3149 | 2921 | <a name="method-fluent-str-plural"></a> |
3150 | ||
3151 | 2922 | #### `plural` |
3152 | 2923 | |
3153 | `plural` 方法将单数形式的字符串转换为复数形式。该此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localization | |
2924 | `plural` 方法将单数形式的字符串转换为复数形式。该此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localizationmd#pluralization-language) | |
3154 | 2925 | |
3155 | 2926 | use Illuminate\Support\Str; |
3156 | 2927 | … | … |
3174 | 2945 | |
3175 | 2946 | // child |
3176 | 2947 | |
3177 | ||
3178 | ||
3179 | 2948 | <a name="method-fluent-str-prepend"></a> |
3180 | ||
3181 | 2949 | #### `prepend` |
3182 | 2950 | |
3183 | 2951 | `prepend` 方法用于在指定字符串的开头插入另一指定字符串: | … | … |
3189 | 2957 | // Laravel Framework |
3190 | 2958 | |
3191 | 2959 | <a name="method-fluent-str-remove"></a> |
3192 | ||
3193 | 2960 | #### `remove` |
3194 | 2961 | |
3195 | 2962 | `remove` 方法用于从字符串中删除给定的值或值数组: | … | … |
3203 | 2970 | 你也可以传递 `false` 作为第二个参数以在删除字符串时忽略大小写。 |
3204 | 2971 | |
3205 | 2972 | <a name="method-fluent-str-replace"></a> |
3206 | ||
3207 | 2973 | #### `replace` |
3208 | 2974 | |
3209 | 2975 | `replace` 方法用于将字符串中的指定字符串替换为另一指定字符串: |
3210 | 2976 | |
3211 | 2977 | use Illuminate\Support\Str; |
3212 | 2978 | |
3213 | $replaced = Str::of('Laravel | |
3214 |
| |
3215 | // Laravel | |
2979 | $replaced = Str::of('Laravel 9.x')->replace('9.x', '10.x'); | |
2980 | ||
2981 | // Laravel 10.x | |
3216 | 2982 | |
3217 | 2983 | <a name="method-fluent-str-replace-array"></a> |
3218 | ||
3219 | 2984 | #### `replaceArray` |
3220 | 2985 | |
3221 | 2986 | `replaceArray` 方法使用数组顺序替换字符串中的给定值: | … | … |
3229 | 2994 | // The event will take place between 8:30 and 9:00 |
3230 | 2995 | |
3231 | 2996 | <a name="method-fluent-str-replace-first"></a> |
3232 | ||
3233 | 2997 | #### `replaceFirst` |
3234 | 2998 | |
3235 | 2999 | `replaceFirst` 方法替换字符串中给定值的第一个匹配项: | … | … |
3241 | 3005 | // a quick brown fox jumps over the lazy dog |
3242 | 3006 | |
3243 | 3007 | <a name="method-fluent-str-replace-last"></a> |
3244 | ||
3245 | 3008 | #### `replaceLast` |
3246 | 3009 | |
3247 | 3010 | `replaceLast` 方法替换字符串中给定值的最后一个匹配项: | … | … |
3252 | 3015 | |
3253 | 3016 | // the quick brown fox jumps over a lazy dog |
3254 | 3017 | |
3255 | ||
3256 | ||
3257 | 3018 | <a name="method-fluent-str-replace-matches"></a> |
3258 | ||
3259 | 3019 | #### `replaceMatches` |
3260 | 3020 | |
3261 | 3021 | `replaceMatches` 方法用给定的替换字符串替换与模式匹配的字符串的所有部分 | … | … |
3278 | 3038 | // '[1][2][3]' |
3279 | 3039 | |
3280 | 3040 | <a name="method-fluent-str-rtrim"></a> |
3281 | ||
3282 | 3041 | #### `rtrim` |
3283 | 3042 | |
3284 | 3043 | `rtrim` 方法修剪给定字符串的右侧: | … | … |
3294 | 3053 | // '/Laravel' |
3295 | 3054 | |
3296 | 3055 | <a name="method-fluent-str-scan"></a> |
3297 | ||
3298 | 3056 | #### `scan` |
3299 | 3057 | |
3300 | `scan` 方法根据 [PHP 函数 sscanf | |
3058 | `scan` 方法根据 [PHP 函数 sscanf](https://www.php.net/manual/en/function.sscanf.php) 支持的格式把字符串中的输入解析为集合: | |
3301 | 3059 | |
3302 | 3060 | use Illuminate\Support\Str; |
3303 | 3061 | … | … |
3306 | 3064 | // collect(['filename', 'jpg']) |
3307 | 3065 | |
3308 | 3066 | <a name="method-fluent-str-singular"></a> |
3309 | ||
3310 | 3067 | #### `singular` |
3311 | 3068 | |
3312 | `singular` 方法将字符串转换为其单数形式。此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localization | |
3069 | `singular` 方法将字符串转换为其单数形式。此函数支持 [Laravel的复数化器支持的任何语言](/docs/laravel/10.x/localizationmd#pluralization-language) : | |
3313 | 3070 | |
3314 | 3071 | use Illuminate\Support\Str; |
3315 | 3072 | … | … |
3321 | 3078 | |
3322 | 3079 | // child |
3323 | 3080 | |
3324 | ||
3325 | ||
3326 | 3081 | <a name="method-fluent-str-slug"></a> |
3327 | ||
3328 | 3082 | #### `slug` {.collection-method} |
3329 | 3083 | |
3330 | 3084 | `slug` 方法从给定字符串生成 URL 友好的 "slug": | … | … |
3336 | 3090 | // laravel-framework |
3337 | 3091 | |
3338 | 3092 | <a name="method-fluent-str-snake"></a> |
3339 | ||
3340 | 3093 | #### `snake` {.collection-method} |
3341 | 3094 | |
3342 | 3095 | `snake` 方法将给定字符串转换为 `snake_case` | … | … |
3348 | 3101 | // foo_bar |
3349 | 3102 | |
3350 | 3103 | <a name="method-fluent-str-split"></a> |
3351 | ||
3352 | 3104 | #### `split` {.collection-method} |
3353 | 3105 | |
3354 | 3106 | split 方法使用正则表达式将字符串拆分为集合: | … | … |
3360 | 3112 | // collect(["one", "two", "three"]) |
3361 | 3113 | |
3362 | 3114 | <a name="method-fluent-str-squish"></a> |
3363 | ||
3364 | 3115 | #### `squish` {.collection-method} |
3365 | 3116 | |
3366 | 3117 | `squish` 方法删除字符串中所有无关紧要的空白,包括字符串之间的空白: | … | … |
3372 | 3123 | // laravel framework |
3373 | 3124 | |
3374 | 3125 | <a name="method-fluent-str-start"></a> |
3375 | ||
3376 | 3126 | #### `start` {.collection-method} |
3377 | 3127 | |
3378 | 3128 | `start` 方法将给定值的单个实例添加到字符串中,前提是该字符串尚未以该值开头: | … | … |
3388 | 3138 | // /this/string |
3389 | 3139 | |
3390 | 3140 | <a name="method-fluent-str-starts-with"></a> |
3391 | ||
3392 | 3141 | #### `startsWith` {.collection-method} |
3393 | 3142 | |
3394 | 3143 | `startsWith` 方法确定给定字符串是否以给定值开头: | … | … |
3400 | 3149 | // true |
3401 | 3150 | |
3402 | 3151 | <a name="method-fluent-str-studly"></a> |
3403 | ||
3404 | 3152 | #### `studly` {.collection-method} |
3405 | 3153 | |
3406 | 3154 | `studly` 方法将给定字符串转换为 `StudlyCase`: | … | … |
3411 | 3159 | |
3412 | 3160 | // FooBar |
3413 | 3161 | |
3414 | ||
3415 | ||
3416 | 3162 | <a name="method-fluent-str-substr"></a> |
3417 | ||
3418 | 3163 | #### `substr` {.collection-method} |
3419 | 3164 | |
3420 | 3165 | `substr` 方法返回由给定的起始参数和长度参数指定的字符串部分: | … | … |
3430 | 3175 | // Frame |
3431 | 3176 | |
3432 | 3177 | <a name="method-fluent-str-substrreplace"></a> |
3433 | ||
3434 | 3178 | #### `substrReplace` {.collection-method} |
3435 | 3179 | |
3436 | 3180 | `substrReplace` 方法在字符串的一部分中替换文本,从第二个参数指定的位置开始替换第三个参数指定的字符数。将 `0` 传递给方法的第三个参数将在指定位置插入字符串,而不替换字符串中的任何现有字符: | … | … |
3446 | 3190 | // The Laravel Framework |
3447 | 3191 | |
3448 | 3192 | <a name="method-fluent-str-swap"></a> |
3449 | ||
3450 | 3193 | #### `swap` {.collection-method} |
3451 | 3194 | |
3452 | 3195 | `swap` 方法使用 PHP 的 `strtr` 函数替换字符串中的多个值: | … | … |
3480 | 3223 | // LARAVEL FRAMEWORK |
3481 | 3224 | |
3482 | 3225 | <a name="method-fluent-str-test"></a> |
3483 | ||
3484 | ||
3485 | 3226 | #### `test` {.collection-method} |
3486 | 3227 | |
3487 | 3228 | `test` 方法确定字符串是否与给定的正则表达式模式匹配: | … | … |
3505 | 3246 | // A Nice Title Uses The Correct Case |
3506 | 3247 | |
3507 | 3248 | <a name="method-fluent-str-trim"></a> |
3508 | ||
3509 | 3249 | #### `trim` {.collection-method} |
3510 | 3250 | |
3511 | 3251 | `trim` 方法修剪给定字符串: | … | … |
3521 | 3261 | // 'Laravel' |
3522 | 3262 | |
3523 | 3263 | <a name="method-fluent-str-ucfirst"></a> |
3524 | ||
3525 | 3264 | #### `ucfirst` {.collection-method} |
3526 | 3265 | |
3527 | 3266 | `ucfirst` 方法返回第一个字符大写的给定字符串 | … | … |
3533 | 3272 | // Foo bar |
3534 | 3273 | |
3535 | 3274 | <a name="method-fluent-str-ucsplit"></a> |
3536 | ||
3537 | 3275 | #### `ucsplit` {.collection-method} |
3538 | 3276 | |
3539 | 3277 | `ucsplit` 方法将给定的字符串按大写字符分割为一个集合: | … | … |
3545 | 3283 | // collect(['Foo', 'Bar']) |
3546 | 3284 | |
3547 | 3285 | <a name="method-fluent-str-upper"></a> |
3548 | ||
3549 | 3286 | #### `upper` {.collection-method} |
3550 | 3287 | |
3551 | 3288 | `upper` 方法将给定字符串转换为大写: | … | … |
3557 | 3294 | // LARAVEL |
3558 | 3295 | |
3559 | 3296 | <a name="method-fluent-str-when"></a> |
3560 | ||
3561 | 3297 | #### `when` {.collection-method} |
3562 | 3298 | |
3563 | 3299 | 如果给定的条件为 `true`,则 `when` 方法调用给定的闭包。闭包将接收一个流畅字符串实例: | … | … |
3574 | 3310 | |
3575 | 3311 | 如果需要,可以将另一个闭包作为第三个参数传递给 `when` 方法。如果条件参数的计算结果为 `false`,则将执行此闭包。 |
3576 | 3312 | |
3577 | ||
3578 | ||
3579 | 3313 | <a name="method-fluent-str-when-contains"></a> |
3580 | ||
3581 | 3314 | #### `whenContains` {.collection-method} |
3582 | 3315 | |
3583 | 3316 | `whenContains` 方法会在字符串包含给定的值的前提下,调用给定的闭包。闭包将接收字符流处理实例: | … | … |
3607 | 3340 | // Tony Stark |
3608 | 3341 | |
3609 | 3342 | <a name="method-fluent-str-when-contains-all"></a> |
3610 | ||
3611 | 3343 | #### `whenContainsAll` {.collection-method} |
3612 | 3344 | |
3613 | 3345 | `whenContainsAll` 方法会在字符串包含所有给定的子字符串时,调用给定的闭包。闭包将接收字符流处理实例: | … | … |
3625 | 3357 | 如有必要,你可以将另一个闭包作为第三个参数传递给 `whenContainsAll` 方法。如果条件参数评估为 `false`,则此闭包将执行。 |
3626 | 3358 | |
3627 | 3359 | <a name="method-fluent-str-when-empty"></a> |
3628 | ||
3629 | 3360 | #### `whenEmpty` {.collection-method} |
3630 | 3361 | |
3631 | 3362 | 如果字符串为空,`whenEmpty` 方法将调用给定的闭包。如果闭包返回一个值,`whenEmpty` 方法也将返回该值。如果闭包不返回值,则将返回字符流处理实例: | … | … |
3639 | 3370 | |
3640 | 3371 | // 'Laravel' |
3641 | 3372 | |
3642 | ||
3643 | ||
3644 | 3373 | <a name="method-fluent-str-when-not-empty"></a> |
3645 | ||
3646 | 3374 | #### `whenNotEmpty` {.collection-method} |
3647 | 3375 | |
3648 | 3376 | 如果字符串不为空,`whenNotEmpty` 方法会调用给定的闭包。如果闭包返回一个值,那么 `whenNotEmpty` 方法也将返回该值。如果闭包没有返回值,则返回字符流处理实例: | … | … |
3657 | 3385 | // 'Laravel Framework' |
3658 | 3386 | |
3659 | 3387 | <a name="method-fluent-str-when-starts-with"></a> |
3660 | ||
3661 | 3388 | #### `whenStartsWith` {.collection-method} |
3662 | 3389 | |
3663 | 3390 | 如果字符串以给定的子字符串开头,`whenStartsWith` 方法会调用给定的闭包。闭包将接收字符流处理实例: | … | … |
3672 | 3399 | // 'Disney World' |
3673 | 3400 | |
3674 | 3401 | <a name="method-fluent-str-when-ends-with"></a> |
3675 | ||
3676 | 3402 | #### `whenEndsWith` {.collection-method} |
3677 | 3403 | |
3678 | 3404 | 如果字符串以给定的子字符串结尾,`whenEndsWith` 方法会调用给定的闭包。闭包将接收字符流处理实例: | … | … |
3687 | 3413 | // 'Disney World' |
3688 | 3414 | |
3689 | 3415 | <a name="method-fluent-str-when-exactly"></a> |
3690 | ||
3691 | 3416 | #### `whenExactly` {.collection-method} |
3692 | 3417 | |
3693 | 3418 | 如果字符串与给定字符串完全匹配,`whenExactly` 方法会调用给定的闭包。闭包将接收字符流处理实例: | … | … |
3702 | 3427 | // 'Laravel' |
3703 | 3428 | |
3704 | 3429 | <a name="method-fluent-str-when-not-exactly"></a> |
3705 | ||
3706 | 3430 | #### `whenNotExactly` {.collection-method} |
3707 | 3431 | |
3708 | 3432 | 如果字符串与给定字符串不完全匹配,`whenNotExactly`方法将调用给定的闭包。闭包将接收字符流处理实例: | … | … |
3716 | 3440 | |
3717 | 3441 | // 'Framework' |
3718 | 3442 | |
3719 | ||
3720 | ||
3721 | 3443 | <a name="method-fluent-str-when-is"></a> |
3722 | ||
3723 | 3444 | #### `whenIs` {.collection-method} |
3724 | 3445 | |
3725 | 3446 | 如果字符串匹配给定的模式,`whenIs` 方法会调用给定的闭包。星号可用作通配符值。闭包将接收字符流处理实例: | … | … |
3734 | 3455 | // 'foo/bar/baz' |
3735 | 3456 | |
3736 | 3457 | <a name="method-fluent-str-when-is-ascii"></a> |
3737 | ||
3738 | 3458 | #### `whenIsAscii` {.collection-method} |
3739 | 3459 | |
3740 | 3460 | 如果字符串是 7 位 ASCII,`whenIsAscii` 方法会调用给定的闭包。闭包将接收字符流处理实例: | … | … |
3749 | 3469 | // 'Laravel' |
3750 | 3470 | |
3751 | 3471 | <a name="method-fluent-str-when-is-ulid"></a> |
3752 | ||
3753 | 3472 | #### `whenIsUlid` {.collection-method} |
3754 | 3473 | |
3755 | 3474 | 如果字符串是有效的 ULID,`whenIsUlid` 方法会调用给定的闭包。闭包将接收字符流处理实例: | … | … |
3763 | 3482 | // '01gd6r36' |
3764 | 3483 | |
3765 | 3484 | <a name="method-fluent-str-when-is-uuid"></a> |
3766 | ||
3767 | 3485 | #### `whenIsUuid` {.collection-method} |
3768 | 3486 | |
3769 | 3487 | 如果字符串是有效的 UUID,`whenIsUuid` 方法会调用给定的闭包。闭包将接收字符流处理实例: | … | … |
3778 | 3496 | // 'a0a2a2d2' |
3779 | 3497 | |
3780 | 3498 | <a name="method-fluent-str-when-test"></a> |
3781 | ||
3782 | 3499 | #### `whenTest` {.collection-method} |
3783 | 3500 | |
3784 | 3501 | 如果字符串匹配给定的正则表达式,`whenTest` 方法会调用给定的闭包。闭包将接收字符流处理实例: | … | … |
3792 | 3509 | |
3793 | 3510 | // 'Laravel Framework' |
3794 | 3511 | |
3795 | ||
3796 | ||
3797 | 3512 | <a name="method-fluent-str-word-count"></a> |
3798 | ||
3799 | 3513 | #### `wordCount` {.collection-method} |
3800 | 3514 | |
3801 | 3515 | `wordCount` 方法返回字符串包含的单词数: | … | … |
3807 | 3521 | ``` |
3808 | 3522 | |
3809 | 3523 | <a name="method-fluent-str-words"></a> |
3810 | ||
3811 | 3524 | #### `words` {.collection-method} |
3812 | 3525 | |
3813 | 3526 | `words` 方法限制字符串中的字数。如有必要,可以指定附加到截断字符串的附加字符串: | … | … |
3819 | 3532 | // Perfectly balanced, as >>> |
3820 | 3533 | |
3821 | 3534 | <a name="urls"></a> |
3822 | ||
3823 | 3535 | ## URLs |
3824 | 3536 | |
3825 | 3537 | <a name="method-action"></a> |
3826 | ||
3827 | 3538 | #### `action()` {.collection-method} |
3828 | 3539 | |
3829 | 3540 | `action` 函数为给定的控制器操作生成 URL: | … | … |
3837 | 3548 | $url = action([UserController::class, 'profile'], ['id' => 1]); |
3838 | 3549 | |
3839 | 3550 | <a name="method-asset"></a> |
3840 | ||
3841 | 3551 | #### `asset()` {.collection-method} |
3842 | 3552 | |
3843 | 3553 | `asset` 函数使用请求的当前方案(HTTP 或 HTTPS)生成 URL: | … | … |
3854 | 3564 | |
3855 | 3565 | #### `route()` {.collection-method} |
3856 | 3566 | |
3857 | `route` 函数为给定的 [命名路由](/docs/laravel/10.x/routing | |
3567 | `route` 函数为给定的 [命名路由](/docs/laravel/10.x/routingmd#named-routes): | |
3858 | 3568 | |
3859 | 3569 | $url = route('route.name'); |
3860 | 3570 | … | … |
3862 | 3572 | |
3863 | 3573 | $url = route('route.name', ['id' => 1]); |
3864 | 3574 | |
3865 | ||
3866 | ||
3867 | 3575 | 默认情况下,`route` 函数会生成一个绝对路径的 URL。 如果想生成一个相对路径 URL,你可以传递 `false` 作为函数的第三个参数: |
3868 | 3576 | |
3869 | 3577 | $url = route('route.name', ['id' => 1], false); |
3870 | 3578 | |
3871 | 3579 | <a name="method-secure-asset"></a> |
3872 | ||
3873 | 3580 | #### `secure_asset()` {.collection-method} |
3874 | 3581 | |
3875 | 3582 | `secure_asset` 函数使用 HTTPS 为静态资源生成 URL: | … | … |
3877 | 3584 | $url = secure_asset('img/photo.jpg'); |
3878 | 3585 | |
3879 | 3586 | <a name="method-secure-url"></a> |
3880 | ||
3881 | 3587 | #### `secure_url()` {.collection-method} |
3882 | 3588 | |
3883 | 3589 | `secure_url` 函数生成给定路径的完全限定 HTTPS URL。 可以在函数的第二个参数中传递额外的 URL 段: | … | … |
3887 | 3593 | $url = secure_url('user/profile', [1]); |
3888 | 3594 | |
3889 | 3595 | <a name="method-to-route"></a> |
3890 | ||
3891 | 3596 | #### `to_route()` {.collection-method} |
3892 | 3597 | |
3893 | `to_route` 函数为给定的 | |
3598 | `to_route` 函数为给定的 [命名路由](/docs/laravel/10.x/routingmd#named-routes) 生成一个 [重定向 HTTP 响应](/docs/laravel/10.x/responsesmd#redirects): | |
3894 | 3599 | |
3895 | 3600 | return to_route('users.show', ['user' => 1]); |
3896 | 3601 | … | … |
3899 | 3604 | return to_route('users.show', ['user' => 1], 302, ['X-Framework' => 'Laravel']); |
3900 | 3605 | |
3901 | 3606 | <a name="method-url"></a> |
3902 | ||
3903 | 3607 | #### `url()` {.collection-method} |
3904 | 3608 | |
3905 | 3609 | `url` 函数生成给定路径的完全限定 URL: | … | … |
3917 | 3621 | $previous = url()->previous(); |
3918 | 3622 | |
3919 | 3623 | <a name="miscellaneous"></a> |
3920 | ||
3921 | 3624 | ## 杂项 |
3922 | 3625 | |
3923 | 3626 | <a name="method-abort"></a> |
3924 | ||
3925 | 3627 | #### `abort()` {.collection-method} |
3926 | 3628 | |
3927 | 使用`abort` 函数抛出一个 | |
3629 | 使用`abort` 函数抛出一个 [HTTP 异常](/docs/laravel/10.x/errorsmd#http-exceptions) 交给 [异常处理](/docs/laravel/10.x/errorsmd#the-exception-handler "异常处理程序") | |
3928 | 3630 | |
3929 | 3631 | abort(403); |
3930 | 3632 | |
3931 | ||
3932 | ||
3933 | 3633 | 你还可以提供应发送到浏览器的异常消息和自定义 HTTP 响应标头: |
3934 | 3634 | |
3935 | 3635 | abort(403, 'Unauthorized.', $headers); |
3936 | 3636 | |
3937 | 3637 | <a name="method-abort-if"></a> |
3938 | ||
3939 | 3638 | #### `abort_if()` {.collection-method} |
3940 | 3639 | |
3941 | 3640 | 如果给定的布尔表达式的计算结果为 `true`,则 `abort_if` 函数会抛出 HTTP 异常: | … | … |
3945 | 3644 | 与 `abort` 方法一样,你还可以提供异常的响应文本作为函数的第三个参数,并提供自定义响应标头数组作为函数的第四个参数。 |
3946 | 3645 | |
3947 | 3646 | <a name="method-abort-unless"></a> |
3948 | ||
3949 | 3647 | #### `abort_unless()` {.collection-method} |
3950 | 3648 | |
3951 | 3649 | 如果给定的布尔表达式的计算结果为 `false`,则 `abort_unless` 函数会抛出 HTTP 异常: | … | … |
3955 | 3653 | 与 `abort` 方法一样,你还可以提供异常的响应文本作为函数的第三个参数,并提供自定义响应标头数组作为函数的第四个参数。 |
3956 | 3654 | |
3957 | 3655 | <a name="method-app"></a> |
3958 | ||
3959 | 3656 | #### `app()` {.collection-method} |
3960 | 3657 | |
3961 | `app` 函数返回 [服务容器](/docs/laravel/10.x/container | |
3658 | `app` 函数返回 [服务容器](/docs/laravel/10.x/containermd) 实例: | |
3962 | 3659 | |
3963 | 3660 | $container = app(); |
3964 | 3661 | … | … |
3967 | 3664 | $api = app('HelpSpot\API'); |
3968 | 3665 | |
3969 | 3666 | <a name="method-auth"></a> |
3970 | ||
3971 | 3667 | #### `auth()` {.collection-method} |
3972 | 3668 | |
3973 | `auth` 函数返回一个 [authenticator](/docs/laravel/10.x/authentication | |
3669 | `auth` 函数返回一个 [authenticator](/docs/laravel/10.x/authenticationmd) 实例。 你可以将它用作 `Auth` 门面的替代品: | |
3974 | 3670 | |
3975 | 3671 | $user = auth()->user(); |
3976 | 3672 | … | … |
3979 | 3675 | $user = auth('admin')->user(); |
3980 | 3676 | |
3981 | 3677 | <a name="method-back"></a> |
3982 | ||
3983 | 3678 | #### `back()` {.collection-method} |
3984 | 3679 | |
3985 | `back` 函数生成一个 [重定向 HTTP 响应](/docs/laravel/10.x/responses | |
3680 | `back` 函数生成一个 [重定向 HTTP 响应](/docs/laravel/10.x/responsesmd#redirects) 到用户之前的位置: | |
3986 | 3681 | |
3987 | 3682 | return back($status = 302, $headers = [], $fallback = '/'); |
3988 | 3683 | |
3989 | 3684 | return back(); |
3990 | 3685 | |
3991 | ||
3992 | ||
3993 | 3686 | <a name="method-bcrypt"></a> |
3994 | ||
3995 | 3687 | #### `bcrypt()` {.collection-method} |
3996 | 3688 | |
3997 | `bcrypt` 函数 [hashes](/docs/laravel/10.x/hashing | |
3689 | `bcrypt` 函数 [hashes](/docs/laravel/10.x/hashingmd) 使用 Bcrypt 的给定值。 您可以使用此函数作为 `Hash` 门面的替代方法: | |
3998 | 3690 | |
3999 | 3691 | $password = bcrypt('my-secret-password'); |
4000 | 3692 | |
4001 | 3693 | <a name="method-blank"></a> |
4002 | ||
4003 | 3694 | #### `blank()` {.collection-method} |
4004 | 3695 | |
4005 | 3696 | `blank` 函数确定给定值是否为「空白」: | … | … |
4020 | 3711 | 对于 `blank` 的反转,请参阅 [`filled`](#method-filled) 方法。 |
4021 | 3712 | |
4022 | 3713 | <a name="method-broadcast"></a> |
4023 | ||
4024 | 3714 | #### `broadcast()` {.collection-method} |
4025 | 3715 | |
4026 | `broadcast` 函数 [broadcasts](/docs/laravel/10.x/broadcasting | |
3716 | `broadcast` 函数 [broadcasts](/docs/laravel/10.x/broadcastingmd) 给定的 [event](/docs/laravel/10.x/eventsmd) 给它的听众: | |
4027 | 3717 | |
4028 | 3718 | broadcast(new UserRegistered($user)); |
4029 | 3719 | |
4030 | 3720 | broadcast(new UserRegistered($user))->toOthers(); |
4031 | 3721 | |
4032 | 3722 | <a name="method-cache"></a> |
4033 | ||
4034 | 3723 | #### `cache()` {.collection-method} |
4035 | 3724 | |
4036 | `cache` 函数可用于从 [cache](/docs/laravel/10.x/cache | |
3725 | `cache` 函数可用于从 [cache](/docs/laravel/10.x/cachemd) 中获取值。 如果缓存中不存在给定的键,将返回一个可选的默认值: | |
4037 | 3726 | |
4038 | 3727 | $value = cache('key'); |
4039 | 3728 | … | … |
4046 | 3735 | cache(['key' => 'value'], now()->addSeconds(10)); |
4047 | 3736 | |
4048 | 3737 | <a name="method-class-uses-recursive"></a> |
4049 | ||
4050 | 3738 | #### `class_uses_recursive()` {.collection-method} |
4051 | 3739 | |
4052 | 3740 | `class_uses_recursive` 函数返回一个类使用的所有特征,包括其所有父类使用的特征: | … | … |
4054 | 3742 | $traits = class_uses_recursive(App\Models\User::class); |
4055 | 3743 | |
4056 | 3744 | <a name="method-collect"></a> |
4057 | ||
4058 | 3745 | #### `collect()` {.collection-method} |
4059 | 3746 | |
4060 | `collect` 函数根据给定值创建一个 [collection](/docs/laravel/10.x/collections | |
3747 | `collect` 函数根据给定值创建一个 [collection](/docs/laravel/10.x/collectionsmd) 实例: | |
4061 | 3748 | |
4062 | 3749 | $collection = collect(['taylor', 'abigail']); |
4063 | 3750 | |
4064 | ||
4065 | ||
4066 | 3751 | <a name="method-config"></a> |
4067 | ||
4068 | 3752 | #### `config()` {.collection-method} |
4069 | 3753 | |
4070 | `config` 函数获取 [configuration](/docs/laravel/10.x/configuration | |
3754 | `config` 函数获取 [configuration](/docs/laravel/10.x/configurationmd) 变量的值。 可以使用「点」语法访问配置值,其中包括文件名和你希望访问的选项。 如果配置选项不存在,可以指定默认值并返回: | |
4071 | 3755 | |
4072 | 3756 | $value = config('app.timezone'); |
4073 | 3757 | … | … |
4078 | 3762 | config(['app.debug' => true]); |
4079 | 3763 | |
4080 | 3764 | <a name="method-cookie"></a> |
4081 | ||
4082 | 3765 | #### `cookie()` {.collection-method} |
4083 | 3766 | |
4084 | `cookie` 函数创建一个新的 [cookie](/docs/laravel/10.x/requests | |
3767 | `cookie` 函数创建一个新的 [cookie](/docs/laravel/10.x/requestsmd#cookies) 实例: | |
4085 | 3768 | |
4086 | 3769 | $cookie = cookie('name', 'value', $minutes); |
4087 | 3770 | |
4088 | 3771 | <a name="method-csrf-field"></a> |
4089 | ||
4090 | 3772 | #### `csrf_field()` {.collection-method} |
4091 | 3773 | |
4092 | `csrf_field` 函数生成一个 HTML `hidden` 输入字段,其中包含 CSRF 令牌的值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blade | |
3774 | `csrf_field` 函数生成一个 HTML `hidden` 输入字段,其中包含 CSRF 令牌的值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blademd): | |
4093 | 3775 | |
4094 | 3776 | {{ csrf_field() }} |
4095 | 3777 | |
4096 | 3778 | <a name="method-csrf-token"></a> |
4097 | ||
4098 | 3779 | #### `csrf_token()` {.collection-method} |
4099 | 3780 | |
4100 | 3781 | `csrf_token` 函数检索当前 CSRF 令牌的值: | … | … |
4102 | 3783 | $token = csrf_token(); |
4103 | 3784 | |
4104 | 3785 | <a name="method-decrypt"></a> |
4105 | ||
4106 | 3786 | #### `decrypt()` {.collection-method} |
4107 | 3787 | |
4108 | `decrypt` 函数 [解密](/docs/laravel/10.x/encryption | |
3788 | `decrypt` 函数 [解密](/docs/laravel/10.x/encryptionmd) 给定的值。 你可以使用此函数作为 `Crypt` 门面的替代方法: | |
4109 | 3789 | |
4110 | 3790 | $password = decrypt($value); |
4111 | 3791 | |
4112 | 3792 | <a name="method-dd"></a> |
4113 | ||
4114 | 3793 | #### `dd()` {.collection-method} |
4115 | 3794 | |
4116 | 3795 | `dd` 函数转储给定的变量并结束脚本的执行: | … | … |
4121 | 3800 | |
4122 | 3801 | 如果你不想停止脚本的执行,请改用 [`dump`](#method-dump) 函数。 |
4123 | 3802 | |
4124 | ||
4125 | 3803 | <a name="method-dispatch"></a> |
4126 | ||
4127 | 3804 | #### `dispatch()` {.collection-method} |
4128 | 3805 | |
4129 | `dispatch` 函数将给定的 [job](/docs/laravel/10.x/queues | |
3806 | `dispatch` 函数将给定的 [job](/docs/laravel/10.x/queuesmd#creating-jobs) 推送到 Laravel [job queue](/docs/laravel/10.x/queuesmd): | |
4130 | 3807 | |
4131 | 3808 | dispatch(new App\Jobs\SendEmails); |
4132 | 3809 | |
4133 | 3810 | <a name="method-dump"></a> |
4134 | ||
4135 | 3811 | #### `dump()` {.collection-method} |
4136 | 3812 | |
4137 | 3813 | `dump` 函数转储给定的变量: | … | … |
4143 | 3819 | 如果要在转储变量后停止执行脚本,请改用 [`dd`](#method-dd) 函数。 |
4144 | 3820 | |
4145 | 3821 | <a name="method-encrypt"></a> |
4146 | ||
4147 | 3822 | #### `encrypt()` {.collection-method} |
4148 | 3823 | |
4149 | `encrypt` 函数 [encrypts](/docs/laravel/10.x/encryption | |
3824 | `encrypt` 函数 [encrypts](/docs/laravel/10.x/encryptionmd) 给定值。 你可以使用此函数作为 `Crypt` 门面的替代方法: | |
4150 | 3825 | |
4151 | 3826 | $secret = encrypt('my-secret-value'); |
4152 | 3827 | |
4153 | 3828 | <a name="method-env"></a> |
4154 | ||
4155 | 3829 | #### `env()` {.collection-method} |
4156 | 3830 | |
4157 | `env` 函数检索 [环境变量](/docs/laravel/10.x/configuration | |
3831 | `env` 函数检索 [环境变量](/docs/laravel/10.x/configurationmd#environment-configuration) 的值或返回默认值: | |
4158 | 3832 | |
4159 | 3833 | $env = env('APP_ENV'); |
4160 | 3834 | … | … |
4164 | 3838 | > 如果你在部署过程中执行 `config:cache` 命令,你应该确保只从配置文件中调用 `env` 函数。 一旦配置被缓存,`.env` 文件将不会被加载,所有对 `env` 函数的调用都将返回 `null`。 |
4165 | 3839 | |
4166 | 3840 | <a name="method-event"></a> |
4167 | ||
4168 | 3841 | #### `event()` {.collection-method} |
4169 | 3842 | |
4170 | `event` 函数将给定的 [event](/docs/laravel/10.x/events | |
3843 | `event` 函数将给定的 [event](/docs/laravel/10.x/eventsmd) 分派给它的监听器: | |
4171 | 3844 | |
4172 | 3845 | event(new UserRegistered($user)); |
4173 | 3846 | |
4174 | 3847 | <a name="method-fake"></a> |
4175 | ||
4176 | 3848 | #### `fake()` {.collection-method} |
4177 | 3849 | |
4178 | 3850 | `fake` 函数解析容器中的 [Faker](https://github.com/FakerPHP/Faker) 单例,这在模型工厂、数据库填充、测试和原型视图中创建假数据时非常有用: | … | … |
4189 | 3861 | @endfor |
4190 | 3862 | ``` |
4191 | 3863 | |
4192 | ||
4193 | ||
4194 | 3864 | 默认情况下,`fake` 函数将使用 `config/app.php` 配置文件中的 `app.faker_locale` 配置选项; 但是,你也可以通过将语言环境传递给 `fake` 函数来指定语言环境。 每个语言环境将解析一个单独的单例: |
4195 | 3865 | |
4196 | 3866 | fake('nl_NL')->name() |
4197 | 3867 | |
4198 | 3868 | <a name="method-filled"></a> |
4199 | ||
4200 | 3869 | #### `filled()` {.collection-method} |
4201 | 3870 | |
4202 | 3871 | `filled` 函数确定给定值是否不是「空白」: | … | … |
4217 | 3886 | 对于 `filled` 的反转,请参阅 [`blank`](#method-blank) 方法。 |
4218 | 3887 | |
4219 | 3888 | <a name="method-info"></a> |
4220 | ||
4221 | 3889 | #### `info()` {.collection-method} |
4222 | 3890 | |
4223 | `info` 函数会将信息写入应用程序的 [log](/docs/laravel/10.x/logging | |
3891 | `info` 函数会将信息写入应用程序的 [log](/docs/laravel/10.x/loggingmd): | |
4224 | 3892 | |
4225 | 3893 | info('Some helpful information!'); |
4226 | 3894 | … | … |
4229 | 3897 | info('User login attempt failed.', ['id' => $user->id]); |
4230 | 3898 | |
4231 | 3899 | <a name="method-logger"></a> |
4232 | ||
4233 | 3900 | #### `logger()` {.collection-method} |
4234 | 3901 | |
4235 | `logger` 函数可用于将 `debug` 级别的消息写入 [log](/docs/laravel/10.x/logging | |
3902 | `logger` 函数可用于将 `debug` 级别的消息写入 [log](/docs/laravel/10.x/loggingmd): | |
4236 | 3903 | |
4237 | 3904 | logger('Debug message'); |
4238 | 3905 | … | … |
4240 | 3907 | |
4241 | 3908 | logger('User has logged in.', ['id' => $user->id]); |
4242 | 3909 | |
4243 | 如果没有值传递给函数,将返回一个 [logger](/docs/laravel/10.x/errors | |
3910 | 如果没有值传递给函数,将返回一个 [logger](/docs/laravel/10.x/errorsmd#logging) 实例: | |
4244 | 3911 | |
4245 | 3912 | logger()->error('You are not allowed here.'); |
4246 | 3913 | |
4247 | 3914 | <a name="method-method-field"></a> |
4248 | ||
4249 | 3915 | #### `method_field()` {.collection-method} |
4250 | 3916 | |
4251 | `method_field` 函数生成一个 HTML `hidden` 输入字段,其中包含表单 HTTP 谓词的欺骗值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blade | |
3917 | `method_field` 函数生成一个 HTML `hidden` 输入字段,其中包含表单 HTTP 谓词的欺骗值。 例如,使用 [Blade 语法](/docs/laravel/10.x/blademd): | |
4252 | 3918 | |
4253 | 3919 | <form method="POST"> |
4254 | 3920 | {{ method_field('DELETE') }} |
4255 | 3921 | </form> |
4256 | 3922 | |
4257 | 3923 | <a name="method-now"></a> |
4258 | ||
4259 | ||
4260 | 3924 | #### `now()` {.collection-method} |
4261 | 3925 | |
4262 | 3926 | `now` 函数为当前时间创建一个新的 `Illuminate\Support\Carbon` 实例: | … | … |
4264 | 3928 | $now = now(); |
4265 | 3929 | |
4266 | 3930 | <a name="method-old"></a> |
4267 | ||
4268 | 3931 | #### `old()` {.collection-method} |
4269 | 3932 | |
4270 | `old` 函数 [retrieves](/docs/laravel/10.x/requests | |
3933 | `old` 函数 [retrieves](/docs/laravel/10.x/requestsmd#retrieving-input) 一个 [old input](/docs/laravel/10.x/requestsmd#old-input) 值闪入Session : | |
4271 | 3934 | |
4272 | 3935 | $value = old('value'); |
4273 | 3936 | … | … |
4282 | 3945 | {{ old('name', $user) }} |
4283 | 3946 | |
4284 | 3947 | <a name="method-optional"></a> |
4285 | ||
4286 | 3948 | #### `optional()` {.collection-method} |
4287 | 3949 | |
4288 | 3950 | `optional` 函数接受任何参数并允许您访问该对象的属性或调用方法。 如果给定对象为「null」,属性和方法将返回「null」而不是导致错误: | … | … |
4298 | 3960 | }); |
4299 | 3961 | |
4300 | 3962 | <a name="method-policy"></a> |
4301 | ||
4302 | 3963 | #### `policy()` {.collection-method} |
4303 | 3964 | |
4304 | `policy` 方法检索给定类的 [policy](/docs/laravel/10.x/authorization | |
3965 | `policy` 方法检索给定类的 [policy](/docs/laravel/10.x/authorizationmd#creating-policies) 实例: | |
4305 | 3966 | |
4306 | 3967 | $policy = policy(App\Models\User::class); |
4307 | 3968 | |
4308 | 3969 | <a name="method-redirect"></a> |
4309 | ||
4310 | 3970 | #### `redirect()` {.collection-method} |
4311 | 3971 | |
4312 | ||
4313 | ||
4314 | `redirect` 函数返回一个[重定向 HTTP 响应](/docs/laravel/10.x/responses#redirects),或者如果不带参数调用则返回重定向器实例: | |
3972 | `redirect` 函数返回一个[重定向 HTTP 响应](/docs/laravel/10.x/responsesmd#redirects),或者如果不带参数调用则返回重定向器实例: | |
4315 | 3973 | |
4316 | 3974 | return redirect($to = null, $status = 302, $headers = [], $https = null); |
4317 | 3975 | … | … |
4320 | 3978 | return redirect()->route('route.name'); |
4321 | 3979 | |
4322 | 3980 | <a name="method-report"></a> |
4323 | ||
4324 | 3981 | #### `report()` {.collection-method} |
4325 | 3982 | |
4326 | `report` 函数将使用您的 [异常处理程序](/docs/laravel/10.x/errors | |
3983 | `report` 函数将使用您的 [异常处理程序](/docs/laravel/10.x/errorsmd#the-exception-handler) 报告异常: | |
4327 | 3984 | |
4328 | 3985 | report($e); |
4329 | 3986 | … | … |
4332 | 3989 | report('Something went wrong.'); |
4333 | 3990 | |
4334 | 3991 | <a name="method-report-if"></a> |
4335 | ||
4336 | 3992 | #### `report_if()` {.collection-method} |
4337 | 3993 | |
4338 | 如果给定条件为「 | |
3994 | 如果给定条件为「true」,「report_if」函数将使用您的 [异常处理程序](/docs/laravel/10.x/errorsmd#the-exception-handler) 报告异常: | |
4339 | 3995 | |
4340 | 3996 | report_if($shouldReport, $e); |
4341 | 3997 | |
4342 | 3998 | report_if($shouldReport, 'Something went wrong.'); |
4343 | 3999 | |
4344 | 4000 | <a name="method-report-unless"></a> |
4345 | ||
4346 | 4001 | #### `report_unless()` {.collection-method} |
4347 | 4002 | |
4348 | 如果给定条件为 `false`,`report_unless` 函数将使用你的 [异常处理程序](/docs/laravel/10.x/errors | |
4003 | 如果给定条件为 `false`,`report_unless` 函数将使用你的 [异常处理程序](/docs/laravel/10.x/errorsmd#the-exception-handler) 报告异常: | |
4349 | 4004 | |
4350 | 4005 | report_unless($reportingDisabled, $e); |
4351 | 4006 | |
4352 | 4007 | report_unless($reportingDisabled, 'Something went wrong.'); |
4353 | 4008 | |
4354 | 4009 | <a name="method-request"></a> |
4355 | ||
4356 | 4010 | #### `request()` {.collection-method} |
4357 | 4011 | |
4358 | `request` 函数返回当前的 [request](/docs/laravel/10.x/requests | |
4012 | `request` 函数返回当前的 [request](/docs/laravel/10.x/requestsmd) 实例或从当前请求中获取输入字段的值: | |
4359 | 4013 | |
4360 | 4014 | $request = request(); |
4361 | 4015 | |
4362 | 4016 | $value = request('key', $default); |
4363 | 4017 | |
4364 | 4018 | <a name="method-rescue"></a> |
4365 | ||
4366 | 4019 | #### `rescue()` {.collection-method} |
4367 | 4020 | |
4368 | `rescue` 函数执行给定的闭包并捕获其执行期间发生的任何异常。 捕获的所有异常都将发送到你的[异常处理程序](/docs/laravel/10.x/errors | |
4021 | `rescue` 函数执行给定的闭包并捕获其执行期间发生的任何异常。 捕获的所有异常都将发送到你的[异常处理程序](/docs/laravel/10.x/errorsmd#the-exception-handler); 但是,请求将继续处理: | |
4369 | 4022 | |
4370 | 4023 | return rescue(function () { |
4371 | 4024 | return $this->method(); |
4372 | 4025 | }); |
4373 | ||
4374 | ||
4375 | 4026 | |
4376 | 4027 | 你还可以将第二个参数传递给「rescue」函数。 如果在执行闭包时发生异常,这个参数将是应该返回的「默认」值: |
4377 | 4028 | … | … |
4386 | 4037 | }); |
4387 | 4038 | |
4388 | 4039 | <a name="method-resolve"></a> |
4389 | ||
4390 | 4040 | #### `resolve()` {.collection-method} |
4391 | 4041 | |
4392 | `resolve` 函数使用 [服务容器](/docs/laravel/10.x/container | |
4042 | `resolve` 函数使用 [服务容器](/docs/laravel/10.x/containermd) 将给定的类或接口名称解析为实例: | |
4393 | 4043 | |
4394 | 4044 | $api = resolve('HelpSpot\API'); |
4395 | 4045 | |
4396 | 4046 | <a name="method-response"></a> |
4397 | ||
4398 | 4047 | #### `response()` {.collection-method} |
4399 | 4048 | |
4400 | `response` 函数创建一个 [response](/docs/laravel/10.x/responses | |
4049 | `response` 函数创建一个 [response](/docs/laravel/10.x/responsesmd) 实例或获取响应工厂的实例: | |
4401 | 4050 | |
4402 | 4051 | return response('Hello World', 200, $headers); |
4403 | 4052 | |
4404 | 4053 | return response()->json(['foo' => 'bar'], 200, $headers); |
4405 | 4054 | |
4406 | 4055 | <a name="method-retry"></a> |
4407 | ||
4408 | 4056 | #### `retry()` {.collection-method} |
4409 | 4057 | |
4410 | 4058 | `retry` 函数尝试执行给定的回调,直到达到给定的最大尝试阈值。 如果回调没有抛出异常,则返回它的返回值。 如果回调抛出异常,它会自动重试。 如果超过最大尝试次数,将抛出异常: | … | … |
4429 | 4077 | // 第一次重试时休眠 100 ms,第二次重试时休眠 200 ms... |
4430 | 4078 | }); |
4431 | 4079 | |
4432 | ||
4433 | ||
4434 | 4080 | 要仅在特定条件下重试,您可以将闭包作为第四个参数传递给 `retry` 函数: |
4435 | 4081 | |
4436 | 4082 | use Exception; | … | … |
4442 | 4088 | }); |
4443 | 4089 | |
4444 | 4090 | <a name="method-session"></a> |
4445 | ||
4446 | 4091 | #### `session()` {.collection-method} |
4447 | 4092 | |
4448 | `session` 函数可用于获取或设置 [session](/docs/laravel/10.x/session | |
4093 | `session` 函数可用于获取或设置 [session](/docs/laravel/10.x/sessionmd) 值: | |
4449 | 4094 | |
4450 | 4095 | $value = session('key'); |
4451 | 4096 | … | … |
4460 | 4105 | session()->put('key', $value); |
4461 | 4106 | |
4462 | 4107 | <a name="method-tap"></a> |
4463 | ||
4464 | 4108 | #### `tap()` {.collection-method} |
4465 | 4109 | |
4466 | 4110 | `tap` 函数接受两个参数:一个任意的 `$value` 和一个闭包。 `$value` 将传递给闭包,然后由 `tap` 函数返回。 闭包的返回值是无关紧要的: | … | … |
4484 | 4128 | // ... |
4485 | 4129 | }); |
4486 | 4130 | |
4487 | ||
4488 | ||
4489 | 4131 | <a name="method-throw-if"></a> |
4490 | ||
4491 | 4132 | #### `throw_if()` {.collection-method} |
4492 | 4133 | |
4493 | 4134 | 如果给定的布尔表达式的计算结果为「真」,则 `throw_if` 函数会抛出给定的异常: | … | … |
4497 | 4138 | throw_if( |
4498 | 4139 | ! Auth::user()->isAdmin(), |
4499 | 4140 | AuthorizationException::class, |
4500 | ' | |
4141 | '你不允许访问此页面。' | |
4501 | 4142 | ); |
4502 | 4143 | |
4503 | 4144 | <a name="method-throw-unless"></a> |
4504 | ||
4505 | 4145 | #### `throw_unless()` {.collection-method} |
4506 | 4146 | |
4507 | 4147 | 如果给定的布尔表达式的计算结果为 `false`,则 `throw_unless` 函数会抛出给定的异常: | … | … |
4511 | 4151 | throw_unless( |
4512 | 4152 | Auth::user()->isAdmin(), |
4513 | 4153 | AuthorizationException::class, |
4514 | ' | |
4154 | '你不允许访问此页面。' | |
4515 | 4155 | ); |
4516 | 4156 | |
4517 | 4157 | <a name="method-today"></a> |
4518 | ||
4519 | 4158 | #### `today()` {.collection-method} |
4520 | 4159 | |
4521 | 4160 | 如果给定的布尔表达式的计算结果为 `false`,则 `throw_unless` 函数会抛出给定的异常: | … | … |
4523 | 4162 | $today = today(); |
4524 | 4163 | |
4525 | 4164 | <a name="method-trait-uses-recursive"></a> |
4526 | ||
4527 | 4165 | #### `trait_uses_recursive()` {.collection-method} |
4528 | 4166 | |
4529 | 4167 | `trait_uses_recursive` 函数返回特征使用的所有 trait: | … | … |
4531 | 4169 | $traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class); |
4532 | 4170 | |
4533 | 4171 | <a name="method-transform"></a> |
4534 | ||
4535 | 4172 | #### `transform()` {.collection-method} |
4536 | 4173 | |
4537 | 4174 | 如果值不是 [blank](#method-blank),则 transform 函数会对给定值执行闭包,然后返回闭包的返回值: | … | … |
4551 | 4188 | // The value is blank |
4552 | 4189 | |
4553 | 4190 | <a name="method-validator"></a> |
4554 | ||
4555 | 4191 | #### `validator()` {.collection-method} |
4556 | 4192 | |
4557 | `validator` 函数使用给定的参数创建一个新的 [validator](/docs/laravel/10.x/validation | |
4193 | `validator` 函数使用给定的参数创建一个新的 [validator](/docs/laravel/10.x/validationmd) 实例。 你可以将它用作 `Validator` 门面的替代品: | |
4558 | 4194 | |
4559 | 4195 | $validator = validator($data, $rules, $messages); |
4560 | ||
4561 | ||
4562 | 4196 | |
4563 | 4197 | <a name="method-value"></a> |
4564 | 4198 | … | … |
4585 | 4219 | // 'Taylor' |
4586 | 4220 | |
4587 | 4221 | <a name="method-view"></a> |
4588 | ||
4589 | 4222 | #### `view()` {.collection-method} |
4590 | 4223 | |
4591 | `view` 函数检索一个 [view](/docs/laravel/10.x/views | |
4224 | `view` 函数检索一个 [view](/docs/laravel/10.x/viewsmd) 实例: | |
4592 | 4225 | |
4593 | 4226 | return view('auth.login'); |
4594 | 4227 | |
4595 | 4228 | <a name="method-with"></a> |
4596 | ||
4597 | 4229 | #### `with()` {.collection-method} |
4598 | 4230 | |
4599 | 4231 | `with` 函数返回给定的值。 如果将闭包作为函数的第二个参数传递,则将执行闭包并返回其返回值: | … | … |
4615 | 4247 | // 5 |
4616 | 4248 | |
4617 | 4249 | <a name="other-utilities"></a> |
4618 | ||
4619 | 4250 | ## 其他 |
4620 | 4251 | |
4621 | 4252 | <a name="benchmarking"></a> |
4622 | ||
4623 | 4253 | ### 基准测试 |
4624 | 4254 | |
4625 | 4255 | 有时你可能希望快速测试应用程序某些部分的性能。 在这些情况下,您可以使用 Benchmark 支持类来测量给定回调完成所需的毫秒数: | … | … |
4638 | 4268 | |
4639 | 4269 | 默认情况下,给定的回调将执行一次(一次迭代),并且它们的持续时间将显示在浏览器/控制台中。 |
4640 | 4270 | |
4641 | ||
4642 | 4271 | 要多次调用回调,你可以将回调应调用的迭代次数指定为方法的第二个参数。 当多次执行回调时,「基准」类将返回在所有迭代中执行回调所花费的平均毫秒数: |
4643 | 4272 | |
4644 | 4273 | Benchmark::dd(fn () => User::count(), iterations: 10); // 0.5 ms |
4645 | 4274 | |
4646 | 4275 | <a name="pipeline"></a> |
4647 | ||
4648 | 4276 | ### 管道 |
4649 | 4277 | |
4650 | 4278 | Laravel 的 Pipeline 门面提供了一种便捷的方式来通过一系列可调用类、闭包或可调用对象「管道」给定输入,让每个类都有机会检查或修改输入并调用管道中的下一个可调用对象: |
4651 | 4279 | |
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) | |
4658 | 4285 | ->through([ |
4659 | 4286 | function (User $user, Closure $next) { |
4660 | 4287 | // ... | … | … |
4668 | 4295 | }, |
4669 | 4296 | ]) |
4670 | 4297 | ->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) 非常相似。 | |
4674 | 4300 | |
4675 | 4301 | 当管道中的最后一个可调用对象调用 `$next` 闭包时,提供给 `then` 方法的可调用对象将被调用。 通常,此可调用对象将简单地返回给定的输入。 |
4676 | 4302 | |
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) | |
4681 | 4306 | ->through([ |
4682 | 4307 | GenerateProfilePhoto::class, |
4683 | 4308 | ActivateSubscription::class, |
4684 | 4309 | SendWelcomeEmail::class, |
4685 | 4310 | ]) |
4686 | 4311 | ->then(fn (User $user) => $user); |
4687 | ||
4688 | ||
4689 | ||
4690 | 4312 | |
4691 | 4313 | <a name="lottery"></a> |
4692 | ||
4693 | 4314 | ### 彩票 |
4694 | 4315 | |
4695 | 4316 | Laravel 的 Lottery 类可用于根据一组给定的赔率执行回调。 当你只想为一定比例的传入请求执行代码时,这会特别有用: | … | … |
4713 | 4334 | ); |
4714 | 4335 | |
4715 | 4336 | <a name="testing-lotteries"></a> |
4716 | ||
4717 | 4337 | #### 测试彩票 |
4718 | 4338 | |
4719 | 4339 | Laravel 提供了一些简单的方法来让你轻松测试应用程序的 Lottery 调用: |