* * * * *
[TOC]
## 简介
默认情况下 Eloquent 返回的都是一个 `Illuminate\Database\Eloquent\Collection` 对象的实例,包含通过 `get` 方法或是访问一个关联来获取到的结果。Eloquent 集合对象继承了 Laravel [集合基类](https://laravel-china.org/docs/laravel/5.4/collections),因此它自然也继承了许多可用于与 Eloquent 模型交互的方法。
当然,所有集合都可以作为迭代器,来让你像遍历一个 PHP 数组一样来遍历一个集合:
~~~
$users = App\User::where('active', 1)->get();
foreach ($users as $user) {
echo $user->name;
}
~~~
然而,集合比数组更强大的地方是其使用了各种 map / reduce 的直观操作。例如,我们移除所有未激活的用户模型和收集其余各个用户的名字:
~~~
$users = App\User::where('active', 1)->get();
$names = $users->reject(function ($user) {
return $user->active === false;
})
->map(function ($user) {
return $user->name;
});
~~~
> {note} 大部分的 Eloquent 集合会返回新的「Eloquent 集合」实例,但是 `pluck`, `keys`, `zip`, `collapse`, `flatten` 和 `flip` 方法会返回 [基础集合](https://laravel-china.org/docs/laravel/5.4/collections) 实例。
>
> 相应的,如果一个 `map` 操作返回一个不包含任何 Eloquent 模型的集合,那么它将会自动转换成基础集合。
## 可用的方法
### 集合对象
所有 Eloquent 集合都继承了基础的 [Laravel 集合](https://laravel-china.org/docs/laravel/5.4/collections) 对象。因此,他们也继承了所有集合类提供的强大的方法:
[all](https://laravel-china.org/docs/laravel/5.4/collections#method-all)
[avg](https://laravel-china.org/docs/laravel/5.4/collections#method-avg)
[chunk](https://laravel-china.org/docs/laravel/5.4/collections#method-chunk)
[collapse](https://laravel-china.org/docs/laravel/5.4/collections#method-collapse)
[combine](https://laravel-china.org/docs/laravel/5.4/collections#method-combine)
[contains](https://laravel-china.org/docs/laravel/5.4/collections#method-contains)
[count](https://laravel-china.org/docs/laravel/5.4/collections#method-count)
[diff](https://laravel-china.org/docs/laravel/5.4/collections#method-diff)
[diffKeys](https://laravel-china.org/docs/laravel/5.4/collections#method-diffkeys)
[each](https://laravel-china.org/docs/laravel/5.4/collections#method-each)
[every](https://laravel-china.org/docs/laravel/5.4/collections#method-every)
[except](https://laravel-china.org/docs/laravel/5.4/collections#method-except)
[filter](https://laravel-china.org/docs/laravel/5.4/collections#method-filter)
[first](https://laravel-china.org/docs/laravel/5.4/collections#method-first)
[flatMap](https://laravel-china.org/docs/laravel/5.4/collections#method-flatmap)
[flatten](https://laravel-china.org/docs/laravel/5.4/collections#method-flatten)
[flip](https://laravel-china.org/docs/laravel/5.4/collections#method-flip)
[forget](https://laravel-china.org/docs/laravel/5.4/collections#method-forget)
[forPage](https://laravel-china.org/docs/laravel/5.4/collections#method-forpage)
[get](https://laravel-china.org/docs/laravel/5.4/collections#method-get)
[groupBy](https://laravel-china.org/docs/laravel/5.4/collections#method-groupby)
[has](https://laravel-china.org/docs/laravel/5.4/collections#method-has)
[implode](https://laravel-china.org/docs/laravel/5.4/collections#method-implode)
[intersect](https://laravel-china.org/docs/laravel/5.4/collections#method-intersect)
[isEmpty](https://laravel-china.org/docs/laravel/5.4/collections#method-isempty)
[keyBy](https://laravel-china.org/docs/laravel/5.4/collections#method-keyby)
[keys](https://laravel-china.org/docs/laravel/5.4/collections#method-keys)
[last](https://laravel-china.org/docs/laravel/5.4/collections#method-last)
[map](https://laravel-china.org/docs/laravel/5.4/collections#method-map)
[max](https://laravel-china.org/docs/laravel/5.4/collections#method-max)
[merge](https://laravel-china.org/docs/laravel/5.4/collections#method-merge)
[min](https://laravel-china.org/docs/laravel/5.4/collections#method-min)
[only](https://laravel-china.org/docs/laravel/5.4/collections#method-only)
[pluck](https://laravel-china.org/docs/laravel/5.4/collections#method-pluck)
[pop](https://laravel-china.org/docs/laravel/5.4/collections#method-pop)
[prepend](https://laravel-china.org/docs/laravel/5.4/collections#method-prepend)
[pull](https://laravel-china.org/docs/laravel/5.4/collections#method-pull)
[push](https://laravel-china.org/docs/laravel/5.4/collections#method-push)
[put](https://laravel-china.org/docs/laravel/5.4/collections#method-put)
[random](https://laravel-china.org/docs/laravel/5.4/collections#method-random)
[reduce](https://laravel-china.org/docs/laravel/5.4/collections#method-reduce)
[reject](https://laravel-china.org/docs/laravel/5.4/collections#method-reject)
[reverse](https://laravel-china.org/docs/laravel/5.4/collections#method-reverse)
[search](https://laravel-china.org/docs/laravel/5.4/collections#method-search)
[shift](https://laravel-china.org/docs/laravel/5.4/collections#method-shift)
[shuffle](https://laravel-china.org/docs/laravel/5.4/collections#method-shuffle)
[slice](https://laravel-china.org/docs/laravel/5.4/collections#method-slice)
[sort](https://laravel-china.org/docs/laravel/5.4/collections#method-sort)
[sortBy](https://laravel-china.org/docs/laravel/5.4/collections#method-sortby)
[sortByDesc](https://laravel-china.org/docs/laravel/5.4/collections#method-sortbydesc)
[splice](https://laravel-china.org/docs/laravel/5.4/collections#method-splice)
[sum](https://laravel-china.org/docs/laravel/5.4/collections#method-sum)
[take](https://laravel-china.org/docs/laravel/5.4/collections#method-take)
[toArray](https://laravel-china.org/docs/laravel/5.4/collections#method-toarray)
[toJson](https://laravel-china.org/docs/laravel/5.4/collections#method-tojson)
[transform](https://laravel-china.org/docs/laravel/5.4/collections#method-transform)
[union](https://laravel-china.org/docs/laravel/5.4/collections#method-union)
[unique](https://laravel-china.org/docs/laravel/5.4/collections#method-unique)
[values](https://laravel-china.org/docs/laravel/5.4/collections#method-values)
[where](https://laravel-china.org/docs/laravel/5.4/collections#method-where)
[whereStrict](https://laravel-china.org/docs/laravel/5.4/collections#method-wherestrict)
[whereIn](https://laravel-china.org/docs/laravel/5.4/collections#method-wherein)
[whereInLoose](https://laravel-china.org/docs/laravel/5.4/collections#method-whereinloose)
[zip](https://laravel-china.org/docs/laravel/5.4/collections#method-zip)
## 自定义集合
如果你需要使用一个自定义的 `Collection` 对象到自己的扩充方法上,则可以在模型中重写 `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` 方法。
- 前言
- 翻译说明
- 发行说明
- 升级说明
- 贡献导引
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Homestead
- Valet
- 核心概念
- 服务容器
- 服务提供者
- Facades
- Contracts
- HTTP层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- 用户认证
- Passport OAuth 认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- 数据填充
- Redis
- Eloquent ORM
- 快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 测试
- 快速入门
- HTTP 测试
- 浏览器测试 Dusk
- 数据库测试
- 测试模拟器
- 官方扩展包
- Cashier 交易工具包
- Envoy 部署工具
- Scout 全文搜索
- Socialite 社会化登录