[TOC]
### **1、簡介**
[Eloquent](http://laravelacademy.org/tags/eloquent "View all posts in Eloquent")?返回的所有的包含多條記錄的[結果集](http://laravelacademy.org/tags/%e7%bb%93%e6%9e%9c%e9%9b%86 "View all posts in 結果集")都是?`Illuminate\Database\Eloquent\[Collection](http://laravelacademy.org/tags/collection "View all posts in Collection")`?對象的實例,包括通過?`get`?方法或者通過訪問關聯關系獲取的結果。Eloquent?[集合](http://laravelacademy.org/tags/%e9%9b%86%e5%90%88 "View all posts in 集合")對象繼承自?[Laravel](http://laravelacademy.org/tags/laravel "View all posts in Laravel")?的集合基類,因此很自然的繼承了很多處理 Eloquent 模型底層數組的方法。
當然,所有集合也是迭代器,允許你像數組一樣對其進行循環:
~~~
$users?=?App\User::where('active',?1)->get();
foreach?($users?as?$user)?{
echo?$user->name;
}
~~~
然而,集合使用直觀的接口提供了各種映射/簡化操作,因此比數組更加強大。例如,我們可以通過以下方式移除所有無效的模型并聚合還存在的用戶的名字:
~~~
$users?=?App\User::where('active',?1)->get();
$names?=?$users->reject(function?($user)?{
return?$user->active?===?false;
})->map(function?($user)?{
return?$user->name;
});
~~~
> 注意:盡管大多數 Eloquent 集合返回的是一個新的 Eloquent 集合實例,但是`pluck`、`keys`、`zip`、`collapse`、`flatten`和`flip`方法返回的是集合基類實例。
### **2、可用方法**
所有的 Eloquent 集合繼承自 Laravel 集合對象基類,因此,它們繼承所有集合基類提供的強大方法:
[all](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_3)
[chunk](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_4)
[collapse](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_5)
[contains](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_6)
[count](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_7)
[diff](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_8)
[each](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_9)
[filter](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_10)
[first](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_11)
[flatten](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_12)
[flip](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_13)
[forget](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_14)
[forPage](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_15)
[get](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_16)
[groupBy](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_17)
[has](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_18)
[implode](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_19)
[intersect](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_20)
[isEmpty](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_21)
[keyBy](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_22)
[keys](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_23)
[last](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_24)
[map](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_25)
[merge](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_26)
[pluck](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_27)
[pop](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_28)
[prepend](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_29)
[pull](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_30)
[push](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_31)
[put](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_32)
[random](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_33)
[reduce](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_34)
[reject](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_35)
[reverse](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_36)
[search](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_37)
[shift](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_38)
[shuffle](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_39)
[slice](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_40)
[sort](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_41)
[sortBy](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_42)
[sortByDesc](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_43)
[splice](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_44)
[sum](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_45)
[take](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_46)
[toArray](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_47)
[toJson](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_48)
[transform](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_49)
[unique](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_50)
[values](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_51)
[where](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_52)
[whereLoose](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_53)
[zip](http://laravelacademy.org/post/178.html#ipt_kb_toc_178_54)
### **3、[自定義](http://laravelacademy.org/tags/%e8%87%aa%e5%ae%9a%e4%b9%89 "View all posts in 自定義")集合**
如果你需要在自己擴展的方法中使用自定義的集合對象,可以重寫模型上的?`newCollection`?方法:
~~~
<?php
namespace App;
use App\CustomCollection;
use Illuminate\Database\Eloquent\Model;
class User extends Model{
/**
* 創建一個新的Eloquent集合實例
*
* @param array $models
* @return \Illuminate\Database\Eloquent\Collection
*/
public function newCollection(array $models = [])
{
return new CustomCollection($models);
}
}
~~~
定義好?`newCollection`?方法后,無論何時 Eloquent 返回該模型的?`Collection`?實例你都會獲取到自定義的集合。如果你想要在應用中的每一個模型中使用自定義集合,需要在模型基類中重寫?`newCollection`?方法。
- 序言
- 發行版本說明
- 升級指南
- 貢獻代碼
- 開始
- 安裝
- 配置
- Laravel Homestead
- 基礎
- HTTP 路由
- HTTP 中間件
- HTTP 控制器
- HTTP 請求
- HTTP 響應
- 視圖
- Blade 模板引擎
- 架構
- 一次請求的生命周期
- 應用目錄結構
- 服務提供者
- 服務容器
- 門面(Facades)
- 數據庫
- 起步
- 查詢構建器
- 遷移
- 填充數據
- Eloquent ORM
- 起步
- 關聯關系
- 集合
- 訪問器&修改器
- 序列化
- 服務
- 用戶認證
- 用戶授權
- Artisan Console
- 訂閱支付實現:Laravel Cashier
- 緩存
- 集合
- 集成前端資源:Laravel Elixir
- 加密
- 錯誤&日志
- 事件
- 文件系統/云存儲
- 哈希
- 輔助函數
- 本地化
- 郵件
- 包開發
- 分頁
- Redis
- 隊列
- Session
- Envoy Task Runner
- 任務調度
- 測試
- 驗證
- 新手入門指南
- 簡單任務管理系統
- 帶用戶功能的任務管理系統