# Collection類
### 介紹
`October\Rain\Support\Collection`類提供一個流暢,方便的包裝用于與數據的數組工作。例如,查看以下代碼。我們將從數組創建一個新的集合實例,`strtoupper`在每個元素上運行該函數,然后刪除所有空元素:
~~~
$collection = new October\Rain\Support\Collection(['stewie', 'brian', null]);
$collection = $collection
->map(function ($name) {
return strtoupper($name);
})
->reject(function ($name) {
return empty($name);
})
;
~~~
在`Collection`類允許您鏈中的方法來執行流利映射和底層數組的減少。通常,每個`Collection`方法都返回一個全新的`Collection`實例。
### [](https://octobercms.com/docs/services/collections#creating-collections)創建收藏
如上所述,將數組傳遞給`October\Rain\Support\Collection`類的構造函數將為給定數組返回一個新實例。因此,創建集合非常簡單:
~~~
$collection = new October\Rain\Support\Collection([1, 2, 3]);
~~~
默認情況下,[數據庫模型的](https://octobercms.com/docs/database/model)集合始終作為`Collection`實例返回;但是,請`Collection`在適合您的應用程序的任何地方隨意使用該類。
### 高階消息
集合還支持“高級消息”,這是對集合執行常見操作的捷徑。提供更高階的信息收集方法有:[`average`](https://octobercms.com/docs/services/collections#method-average),[`avg`](https://octobercms.com/docs/services/collections#method-avg),[`contains`](https://octobercms.com/docs/services/collections#method-contains),[`each`](https://octobercms.com/docs/services/collections#method-each),[`every`](https://octobercms.com/docs/services/collections#method-every),[`filter`](https://octobercms.com/docs/services/collections#method-filter),[`first`](https://octobercms.com/docs/services/collections#method-first),[`flatMap`](https://octobercms.com/docs/services/collections#method-flatmap),[`groupBy`](https://octobercms.com/docs/services/collections#method-groupby),[`keyBy`](https://octobercms.com/docs/services/collections#method-keyby),[`map`](https://octobercms.com/docs/services/collections#method-map),[`max`](https://octobercms.com/docs/services/collections#method-max),[`min`](https://octobercms.com/docs/services/collections#method-min),[`partition`](https://octobercms.com/docs/services/collections#method-partition),[`reject`](https://octobercms.com/docs/services/collections#method-reject),[`some`](https://octobercms.com/docs/services/collections#method-some),[`sortBy`](https://octobercms.com/docs/services/collections#method-sortby),[`sortByDesc`](https://octobercms.com/docs/services/collections#method-sortbydesc),[`sum`](https://octobercms.com/docs/services/collections#method-sum),和[`unique`](https://octobercms.com/docs/services/collections#method-unique)。
每個高階消息都可以作為集合實例上的動態屬性來訪問。例如,讓我們使用`each`高階消息在集合內的每個對象上調用一個方法:
~~~
$users = User::where('votes', '>', 500)->get();
$users->each->markAsVip();
~~~
同樣,我們可以使用`sum`較高順序的消息來收集用戶集合的“票數”總數:
~~~
$users = User::where('group', 'Development')->get();
return $users->sum->votes;
~~~
- 基本說明
- 基本操作
- October cms 安裝
- 后臺控制器路徑
- 圖標
- 獲取安裝網上的插件/主題
- 插件構造器使用
- 定時任務
- October后臺控制器
- vscode編輯器
- ajax操作
- 使用
- ajax更新組件
- ajax屬性API
- JavaScript API
- ajax綜合使用
- 主題
- 多語言主題
- 安裝市場主題
- 主題程序處理
- 主題
- 頁面
- 部件
- 布局
- 內容
- 組件
- 媒體
- 主題表單操作
- 表單使用
- 表單后端程序處理
- 插件
- 自定義插件
- 插件說明
- 插件導航條
- 插件數據庫設置
- 插件的設置管理
- 插件的配置文件config
- 組件
- app服務
- app容器
- 擴展行為
- 緩存
- Collection類
- Lazy Collections
- Collection方法
- 助手函數
- 數組助手函數
- 路徑助手函數
- 玄樂助手函數
- 其他助手函數
- 錯誤與記錄
- 事件處理
- HTML頁面
- 文件與目錄操作
- 散列和加密
- 郵件
- 郵件內容
- 郵件發送
- 分頁
- 模板解析器
- 動態解析器語法
- 隊列消息
- 請求與輸入
- 響應
- 視圖
- 路由器
- 配置
- 驗證操作
- 處理錯誤消息
- 錯誤消息與視圖
- 可用的驗證規則
- 有條件的驗證規則
- 驗證數組
- 錯誤消息
- 自定義驗證規則
- 模型操作
- 定義模型與其屬性
- 檢索模型
- 插入與更新
- 刪除模型
- 查詢范圍
- 事件操作
- 關聯操作
- 定義關系
- 關系類型
- 多肽關系
- 關系查詢
- 渴望加載
- 插入模型
- 數據庫操作
- 基本用法
- 數據表結構
- 查詢連貫操作
- 結果檢索
- select子句
- 插入更新
- where子句
- 排序,分組,限制和偏移
- 文件附件
- Collection操作
- 屬性操作
- 系列化json
- 數據庫屬性
- 數據庫行為
- 控制器
- 后臺控制器定義
- 后臺頁面
- 后臺組件
- 后臺表單
- 表單組件
- 表單視圖
- 表單行為
- 后臺列表
- 列表行為
- 列表過濾器
- 可用列類型
- 關系行為
- 關系行為類型
- 擴展關系行為
- 列表排序操作
- 導入導出操作
- 用于與權限
- corlate模板修改
- 修改頂部導航
- laravel問題
- 控制器不存在
- 控制器
- 路由組
- laravel筆記
- laravel 安裝
- 偽靜態配置
- 依賴注入 & 控制器
- 中間件
- 路由文件
- 視圖