RandyChan 2年前

修改理由:

`make() to `make()`

相关信息:


此投稿由 Summer 2年前 合并。

标题修改:

+ 集合

内容修改:

红色背景 为原始内容

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

OldNewDifferences
1  
21# 集合
32
43- [介绍](#introduction)
 
5453
5554   // ['FIRST', 'SECOND']
5655
57 
58 
 56
 57
5958通常,您应该在[service provider](/docs/laravel/9.x/providers)的 `boot` 方法中声明集合宏。
6059
6160<a name="macro-arguments"></a>
 
232231
233232</div>
234233
235 
236 
 234
 235
237236<a name="method-listing"></a>
238237## 方法列表
239238
 
322321<a name="method-collapse"></a>
323322#### `collapse()` {.collection-method}
324323
325 
326 
 324
 325
327326`collapse` 方法将数组集合折叠成一个单一的平面集合:
328327
329328   $collection = collect([
 
412411
413412   // false
414413
415 
416 
 414
 415
417416或者,您可以将字符串传递给 `contains` 方法,以确定集合是否包含给定的项目值:
418417
419418   $collection = collect(['name' => 'Desk', 'price' => 100]);
 
484483
485484   // ['gmail.com' => 2, 'yahoo.com' => 1]
486485
487 
488 
 486
 487
489488<a name="method-crossjoin"></a>
490489#### `crossJoin()` {.collection-method}
491490
 
583582   // ['color' => 'orange', 'remain' => 6]
584583
585584<a name="method-diffkeys"></a>
586 
587 
 585
 586
588587#### `diffKeys()` {.collection-method}
589588
590589`diffKeys` 方法将集合与另一个集合或基于其键的普通 PHP `array` 进行比较。 此方法将返回给定集合中不存在的原始集合中的键/值对:
 
666665
667666如果要在转储集合后停止执行脚本,请改用 [`dd`](#method-dd) 方法。
668667
669 
670 
 668
 669
671670<a name="method-duplicates"></a>
672671#### `duplicates()` {.collection-method}
673672
 
751750
752751   // true
753752
754 
755 
 753
 754
756755<a name="method-except"></a>
757756#### `except()` {.collection-method}
758757
 
834833
835834   // ['name' => 'Diego', 'age' => 23]
836835
837 
838 
 836
 837
839838与 [where](#method-where) 方法一样,您可以将一个参数传递给 `firstWhere` 方法。 在这种情况下,`firstWhere` 方法将返回给定项目键值为“真”的第一个项目:
840839
841840   $collection->firstWhere('age');
 
922921
923922   // ['taylor' => 'name', 'laravel' => 'framework']
924923
925 
926 
 924
 925
927926<a name="method-forget"></a>
928927#### `forget()`
929928
 
10061005       ]
10071006   */
10081007
1009 
1010 
 1008
 1009
10111010您可以传递回调,而不是传递字符串 `key`。 回调应返回您希望通过以下方式键入组的值:
10121011
10131012   $grouped = $collection->groupBy(function ($item, $key) {
 
10991098
11001099   // Desk, Chair
11011100
1102 
1103 
 1101
 1102
11041103如果集合包含简单的字符串或数值,则应将“胶水”作为唯一参数传递给该方法:
11051104
11061105   collect([1, 2, 3, 4, 5])->implode('-');
 
11681167   collect(['a'])->join(', ', ' and '); // 'a'
11691168   collect([])->join(', ', ' and '); // ''
11701169
1171 
1172 
 1170
 1171
11731172<a name="method-keyby"></a>
11741173#### `keyBy()` {.collection-method}
11751174
 
12441243
12451244静态`macro()`方法允许您在运行时向“集合”类添加方法。 有关详细信息,请参阅有关 [扩展集合](#extending-collections) 的文档。
12461245
1247 
1248 
 1246
 1247
12491248<a name="method-make"></a>
1250 #### `make()
 1249#### `make()`
12511250
12521251静态 `make` 方法可以创建一个新的集合实例。请参照 [创建集合](#creating-collections) 部分。
12531252
 
13491348
13501349   // ['John Doe', 'Jane Doe']
13511350
1352 
1353 
 1351
 1352
13541353<a name="method-mapwithkeys"></a>
13551354#### `mapWithKeys()` {.collection-method}
13561355
 
14561455
14571456   // ['product_id' => [1, 2], 'price' => [100, 200], 'discount' => false]
14581457
1459 
1460 
 1458
 1459
14611460<a name="method-min"></a>
14621461#### `min()` {.collection-method}
14631462
 
15531552
15541553   // [0, 0, 'A', 'B', 'C']
15551554
1556 
1557 
 1555
 1556
15581557<a name="method-partition"></a>
15591558#### `partition()`
15601559
 
16781677
16791678   // ['Rosa', 'Judith']
16801679
1681 
1682 
 1680
 1681
16831682如果存在重复键,则将最后一个匹配元素插入到 plucked 集合中:
16841683
16851684   $collection = collect([
 
17971796
17981797   // 4 - (retrieved randomly)
17991798
1800 
1801 
 1799
 1800
18021801您可以将一个整数传递给“random”,以指定要随机检索的项目数。 当明确传递您希望接收的项目数时,始终返回项目集合:
18031802
18041803   $random = $collection->random(3);
 
18781877           return [$creditsRemaining, $batch];
18791878       }, $creditsAvailable, collect());
18801879
1881 
1882 
 1880
 1881
18831882<a name="method-reduce-spread"></a>
18841883#### `reduceSpread()` {.collection-method}
18851884
 
19721971       ]
19731972   */
19741973
1975 
1976 
 1974
 1975
19771976<a name="method-search"></a>
19781977#### `search()` {.collection-method}
19791978
 
20682067
20692068   // [[1, 2, 3], [3, 4, 5]]
20702069
2071 
2072 
 2070
 2071
20732072<a name="method-skip"></a>
20742073#### `skip()` {.collection-method}
20752074
 
21502149
21512150默认情况下,返回的切片将保留键。 如果您不希望保留原始键,可以使用 [`values`](#method-values) 方法重新索引它们。
21522151
2153 
2154 
 2152
 2153
21552154<a name="method-sole"></a>
21562155#### `sole()` {.collection-method}
21572156
 
22082207
22092208> 技巧:如果您需要对嵌套数组或对象的集合进行排序,请参阅 [`sortBy`](#method-sortby) 和 [`sortByDesc`](#method-sortbydesc) 方法。
22102209
2211 
2212 
 2210
 2211
22132212<a name="method-sortby"></a>
22142213#### `sortBy()` {.collection-method}
22152214
 
23002299       ]
23012300   */
23022301
2303 
2304 
 2302
 2303
23052304当按多个属性对集合进行排序时,您还可以提供定义每个排序操作的闭包:
23062305
23072306   $collection = collect([
 
23982397       ]
23992398   */
24002399
2401 
2402 
 2400
 2401
24032402回调必须是返回小于、等于或大于零的整数的比较函数。 有关更多信息,请参阅 [`uksort`](https://www.php.net/manual/en/function.uksort.php#refsect1-function.uksort-parameters) 上的 PHP 文档,这是 PHP 函数 `sortKeysUsing` 方法在内部使用。
24042403
24052404<a name="method-splice"></a>
 
24932492
24942493   // 1272
24952494
2496 
2497 
 2495
 2496
24982497此外,您可以传递自己的闭包来确定要对集合的哪些值求和:
24992498
25002499   $collection = collect([
 
25902589
25912590   // 1
25922591
2593 
2594 
 2592
 2593
25952594<a name="method-times"></a>
25962595#### `times()`
25972596
 
26852684       ]
26862685   */
26872686
2688 
2689 
 2687
 2688
26902689<a name="method-union"></a>
26912690#### `union()`
26922691
 
27552754
27562755> 技巧:这个方法的行为在使用 [Eloquent 集合](/docs/laravel/9.x/eloquent-collections#method-unique) 时被修改。
27572756
2758 
2759 
 2757
 2758
27602759<a name="method-uniquestrict"></a>
27612760#### `uniqueStrict()`
27622761
 
28642863
28652864   // [1, 2, 3, 4]
28662865
2867 
2868 
 2866
 2867
28692868可以将第二个回调传递给该 `when` 方法。`when` 当给方法的第一个参数计算结果为时,将执行第二个回调 `false`:
28702869
28712870   $collection = collect([1, 2, 3]);
 
29662965
29672966与 `whenNotEmpty` 相反的方法,请查看 [`whenEmpty`](#method-whenempty) 方法。
29682967
2969 
2970 
 2968
 2969
29712970<a name="method-where"></a>
29722971#### `where()`
29732972
 
30653064       ]
30663065   */
30673066
3068 
3069 
 3067
 3068
30703069`whereIn` 方法在检查项目值时使用 "loose" 比较,这意味着具有整数值的字符串将被视为等于相同值的整数。 使用 [`whereInStrict`](#method-whereinstrict) 方法使用“strict”比较进行过滤。
30713070
30723071<a name="method-whereinstrict"></a>
 
31433142
31443143`whereNotIn` 方法在检查项目值时使用“loose”比较,这意味着具有整数值的字符串将被视为等于具有相同值的整数。 使用 [`whereNotInStrict`](#method-wherenotinstrict) 方法使用“strict”比较进行过滤。
31453144
3146 
3147 
 3145
 3146
31483147<a name="method-wherenotinstrict"></a>
31493148#### `whereNotInStrict()`
31503149
 
32373236
32383237集合也提供对「高阶消息传递」的支持,即集合常见操作的快捷方式。支持高阶消息传递的集合方法有: [`average`](#method-average),[`avg`](#method-avg),[`contains`](#method-contains), [`each`](#method-each),[`every`](#method-every),[`filter`](#method-filter), [`first`](#method-first),[`flatMap`](#method-flatmap),[`groupBy`](#method-groupby),[`keyBy`](#method-keyby),[`map`](#method-map),[`max`](#method-max), [`min`](#method-min),[`partition`](#method-partition),[`reject`](#method-reject), [`skipUntil`](#method-skipuntil),[`skipWhile`](#method-skipwhile),[`some`](#method-some),[`sortBy`](#method-sortby),[`sortByDesc`](#method-sortbydesc), [`sum`](#method-sum),[`takeUntil`](#method-takeuntil),[`takeWhile`](#method-takewhile) 和 [`unique`](#method-unique)。
32393238
3240 
3241 
 3239
 3240
32423241每个高阶消息都可以作为集合实例上的动态属性进行访问。 例如,让我们使用 `each` 高阶消息来调用集合中每个对象的方法:
32433242
32443243   use App\Models\User;
 
32803279       // Process the log entry...
32813280   });
32823281
3283 
3284 
 3282
 3283
32853284或者,假设您需要遍历 10,000 个 Eloquent 模型。 使用传统 Laravel 集合时,所有 10,000 个 Eloquent 模型必须同时加载到内存中:
32863285
32873286   use App\Models\User;
 
34373436
34383437> 注意:改变集合的方法(例如 `shift`、`pop`、`prepend` 等)在 `LazyCollection` 类中**不**可用。
34393438
3440 
3441 
 3439
 3440
34423441<a name="lazy-collection-methods"></a>
34433442### 惰性集合方法
34443443
 
35153514   // 前 5 个用户数据从缓存中获取
35163515   // 剩余的(15个)用户数据从数据库中查询
35173516   $users->take(20)->all();
3518 ```
 3517```
35193518