# Laravel 的集合 Collection
## 簡介
`Illuminate\Support\Collection` 類提供了一個更具可讀性的、更便于處理數組數據的封裝,具體例子看下面的代碼。
我們使用了 `collect` 函數從數組中創建新的集合實例,對其中的每個元素運行 `strtoupper` 函數之后再移除所有的空元素:
```
$collection = collect(['taylor', 'abigail', null])->map(function ($name) {
return strtoupper($name);
})
->reject(function ($name) {
return empty($name);
});
```
正如你看到的,`Collection` 類允許你鏈式調用其方法,以達到在底層數組上優雅地執行 map 和 reject 操作。一般來說,集合是不可改變的,這意味著每個 `Collection` 方法都會返回一個全新的 `Collection` 實例。
## 創建集合
如上所述,輔助函數 `collect` 會為給定的數組返回一個新的 `Illuminate\Support\Collection` 實例。也就是說,創建一個集合就這么簡單:
```
$collection = collect([1, 2, 3]);
```
> 默認情況下, [Eloquent](https://laravel.com/docs/5.5/eloquent) 查詢的結果返回的內容都是 `Collection` 實例。
## 可用的方法
接下來的內容,我們會探討 `Collection` 類每個可用的方法。**記住,所有方法都可以以方法鏈的形式優雅地操縱數組。**而且,幾乎所有的方法都會返回新的 `Collection` 實例,允許你在必要時保存集合的原始副本。
| 方法名 | 釋義 |
| :--- | :--- |
| all [詳細](/collections/all.md) | 返回該集合表示的底層**數組** |
| average [詳細](/collections/avg.md) | 方法`avg()`的別名 |
| avg [詳細](/collections/avg.md) | 返回給定鍵的**平均值** |
| chunk [詳細](/collections/chuck.md) | 將集合拆成多個指定大小的小集合 |
| collapse [詳細](/collections/collapse.md) | 將多個數組合并成一個 |
| combine [詳細](/collections/combine.md) | 將一個集合的值作為「鍵」,再將另一個數組或者集合的值作為「值」合并成一個集合 |
| contains [詳細](/collections/contains.md) | 判斷集合是否包含給定的項目 |
| containsStrict [詳細](/collections/containsStrict.md) | 使用「嚴格模式」來比較所有值 |
| count [詳細](/collections/count.md) | 返回該集合內的項目總數 |
| diff [詳細](/collections/diff.md) | 基于值求差集 |
| diffAssoc [詳細](/collections/diffAssoc.md) | 基于鍵值對求差集 |
| diffKeys [詳細](/collections/diffKeys.md) | 基于鍵求差集 |
| each [詳細](/collections/each.md) | 迭代集合中的內容并將其傳遞到回調函數中 |
| every [詳細](/every) | 可用于驗證集合中每一個元素都通過給定的真實測試 |
| except [詳細](/collections/except.md) | 返回集合中除了指定鍵以外的所有項目 |
| filter [詳細](/collections/filter.md) | 使用給定的回調函數過濾集合的內容,只留下那些通過給定真實測試的內容 |
| first [詳細](/collections/first.md) | 返回集合中通過給定真實測試的第一個元素 |
| flatMap [詳細](/collections/flatMap.md) | 遍歷集合并將其中的每個值傳遞到給定的回調 |
| flatten [詳細](/collections/flatten.md)| 將多維集合轉為一維 |
| flip [詳細](/collections/flip.md) | 將集合中的鍵和對應的數值進行互換 |
| forget [詳細](/collections/forget.md) | 通過給定的鍵來移除掉集合中對應的內容 |
| forPage [詳細](/collections/forPage.md) | 返回給定頁碼上顯示的項目的新集合 |
| get [詳細](/collections/get.md) | 返回給定鍵的項目 |
| groupBy [詳細](/collections/groupBy.md) | 根據給定的鍵對集合內的項目進行分組 |
| has [詳細](/collections/has.md) | 判斷集合中是否存在給定的鍵 |
| implode [詳細](/collections/implode.md) | 合并集合中的項目 |
| intersect [詳細](/collections/intersect.md) | 從原集合中刪除不在給定數組或集合中的任何值 |
| intersectKey [詳細](/collections/intersectKey.md) | 刪除原集合中不存在于給定數組或集合中的任何鍵 |
| isEmpty [詳細](/collections/isEmpty.md) | 判斷集合是否為空 |
| isNotEmpty [詳細](/collections/isNotEmpty.md) | 判斷集合是否不為空 |
| keyBy [詳細](/collections/keyBy.md) | 以給定的鍵作為集合的鍵 |
| keys [詳細](/collections/keys.md) | 返回集合的所有鍵 |
| last [詳細](/collections/last.md) | 返回集合中通過給定真實測試的最后一個元素 |
| map [詳細](/collections/map.md) | 遍歷集合并將每一個值傳入給定的回調 |
| mapWithKeys [詳細](/collections/mapWithKeys.md) | 遍歷集合并將每個值傳入給定的回調 |
| max [詳細](/collections/max.md) | 返回給定**鍵**的最大值 |
| median [詳細](/collections/median.md) | 方法返回給定**鍵**的中間值 |
| merge [詳細](/collections/merge.md) | 將給定數組或集合合并到原集合 |
| min [詳細](/collections/min.md) | 返回給定鍵的最小值 |
| mode [詳細](/collections/mode.md) | 返回給定**鍵**的[眾數值](https://baike.baidu.com/item/%E4%BC%97%E6%95%B0/44796 "百度百科-眾數值") |
| nth [詳細](/collections/nth.md) | 創建由每隔`n`個元素組成一個新的集合 |
| only [詳細](/collections/only.md) | 返回集合中給定鍵的所有項目 |
| partition [詳細](/collections/partition.md) | 配合`list()`方法區分回調函數滿足和不滿足的數據 |
| pipe [詳細](/collections/pipe.md) | 將集合傳給給定的回調并返回結果 |
| pluck [詳細](/collections/pluck.md) | 獲取集合中給定鍵對應的所有值 |
| pop [詳細](/collections/pop.md) | 移除并返回集合中的最后一個項目 |
| prepend [詳細](/collections/prepend.md) | 將給定的值添加到集合的開頭 |
| pull [詳細](/collections/pull.md) | 把給定鍵對應的值從集合中移除并返回 |
| push [詳細](/collections/push.md) | 把給定值添加到集合的末尾 |
| put [詳細](/collections/put.md) | 在集合內設置給定的鍵值對 |
| random [詳細](/collections/random.md) | 從集合中返回一個隨機項 |
| reduce [詳細](/collections/reduce.md) | 將每次迭代的結果傳遞給下一次迭代直到集合減少為單個值 |
| reject [詳細](/collections/reject.md) | 使用指定的回調過濾集合 |
| reverse [詳細](/collections/reverse.md) | 倒轉集合中項目的順序 |
| search [詳細](/collections/search.md) | 搜索給定的值并返回它的鍵 |
| shift [詳細](/collections/shift.md) | 移除并返回集合的第一個項目 |
| shuffle [詳細](/collections/shuffle.md) | 隨機排序集合中的項目 |
| slice [詳細](/collections/slice.md) | 返回集合中給定值后面的部分 |
| sort [詳細](/collections/sort.md) | 保留原數組的鍵,對集合進行排序 |
| sortBy [詳細](/collections/sortBy.md) | 以給定的鍵對集合進行排序 |
| sortByDesc [詳細](/collections/sortByDesc.md) | 與[sortBy](/collections/sortBy.md)一樣,以相反的順序來對集合進行排序 |
| splice [詳細](/collections/splice.md) | 刪除并返回從給定值后的內容,原集合也會受到影響 |
| split [詳細](/collections/split.md) | 將集合按給定的值拆分 |
| sum [詳細](/collections/sum.md) | 返回集合內所有項目的總和 |
| take [詳細](/collections/take.md) | 返回給定數量項目的新集合 |
| tap [詳細](/collections/tap.md) | 將集合傳遞給回調,在特定點「tap」集合 |
| times [詳細](/collections/times.md) | 通過回調在給定次數內創建一個新的集合 |
| toArray [詳細](/collections/toArray.md) | 將集合轉換成 PHP 數組 |
| toJson [詳細](/collections/toJson.md) | 將集合轉換成 JSON 字符串 |
| transform [詳細](/collections/transform.md) | 迭代集合并對集合內的每個項目調用給定的回調 |
| union [詳細](/collections/union.md) | 將給定的數組添加到集合中 |
| unique [詳細](/collections/unique.md) | 返回集合中所有唯一的項目 |
| uniqueStrict [詳細](/collections/uniqueStrict.md) | 使用嚴格模式返回集合中所有唯一的項目 |
| values [詳細](/collections/values.md) | 返回鍵被重置為連續編號的新集合 |
| when [詳細](/collections/when.md) | 當傳入的第一個參數為 true 的時,將執行給定的回調 |
| where [詳細](/collections/where.md) | 通過給定的鍵值過濾集合 |
| whereStrict [詳細](/collections/whereStrict.md) | 使用嚴格模式通過給定的鍵值過濾集合 |
| whereIn [詳細](/collections/whereIn.md) | 通過給定的鍵值數組來過濾集合 |
| whereInStrict [詳細](/collections/whereStrict.md) | 使用嚴格模式通過給定的鍵值數組來過濾集合 |
| whereNotIn [詳細](/collections/whereNotIn.md) | 集合中不包含的給定鍵值對進行匹配 |
| whereNotInStrict [詳細](/collections/whereNotInStrict.md) | 使用嚴格模式通過集合中不包含的給定鍵值對進行匹配 |
| zip [詳細](/collections/zip.md) | 將給定數組的值與相應索引處的原集合的值合并在一起 |
## 在項目中單獨使用
### 安裝
Laravel中的Collection使用Composer管理,所以我們可以在項目中使用composer安裝到非Laravel項目中,比如我們新建一個collections目錄,通過下面使用命令安裝
```
mkdir collections && cd collections
composer require illuminate/support
```
執行完上面的命令將得到所需要的package。
### 使用
```
<?php
// 引入package
require __DIR__ . '/vendor/autoload.php';
```
## 其他
如果在js中也需要使用類似的數組操作,可以參考 [ecrmnn/collect.js](https://github.com/ecrmnn/collect.js) 的相關操作。
- 介紹
- Laravel5發送郵件使用Service隔離業務
- 如何使用Repository模式
- 如何使用Service模式
- 如何使用Presenter模式
- Laravel 5.* 執行遷移文件報錯:Specified key was too long error
- EloquentORM關聯關系
- EloquentORM關聯關系之一對一
- EloquentORM關聯關系之一對多
- EloquentORM關聯關系之遠層一對多
- EloquentORM關聯關系之多對多
- EloquentORM關聯關系之多態關聯
- EloquentORM關聯關系之多對多多態關聯
- Laravel測試
- Laravel中涉及認證跳轉地址的修改的地方
- Laravel中Collection的基本使用
- all
- avg
- chuck
- collapse
- combine
- contains
- containsStrict
- count
- diff
- diffAssoc
- diffKeys
- each
- every
- except
- filter
- first
- flatMap
- flatten
- flip
- forget
- forPage
- get
- groupBy
- has
- implode
- intersect
- intersectKey
- isEmpty
- isNotEmpty
- keyBy
- keys
- last
- map
- mapWithKeys
- max
- median
- merge
- min
- mode
- nth
- only
- partition
- pipe
- pluck
- pop
- prepend
- pull
- push
- put
- random
- reduce
- reject
- reverse
- search
- shift
- shuffle
- slice
- sort
- sortBy
- sortByDesc
- splice
- split
- sum
- take
- tap
- times
- toArray
- toJson
- transform
- union
- unique
- uniqueStrict
- values
- when
- where
- whereStrict
- whereIn
- whereInStrict
- whereNotIn
- whereNotInStrict
- zip
- Laravel中Collection的實際使用
- collection中sum求和
- collection格式化計算數據
- collection格式化計算數據計算github事件得分總和
- collection格式化markdown數據列表
- collection格式化計算兩個數組的數據
- collection中reduce創建lookup數組
- TODO