<article><h1>Eloquent: 集合</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#available-methods">可用的方法</a></li><li><a href="#custom-collections">自定义集合</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>默认情况下 Eloquent 返回的都是一个 <code class=" language-php">Illuminate\<span class="token package">Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Collection</span></code> 对象的实例,包含通过 <code class=" language-php">get</code> 方法或是访问一个关联来获取到的结果。Eloquent 集合对象继承了 Laravel <a href="/docs/5.4/collections">集合基类</a>,因此它自然也继承了许多可用于与 Eloquent 模型交互的方法。</p><p>当然,所有集合都可以作为迭代器,来让你像遍历一个 PHP 数组一样来遍历一个集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">where<span class="token punctuation">(</span></span><span class="token string">'active'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">echo</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">name</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>然而,集合比数组更强大的地方是其使用了各种 map / reduce 的直观操作。例如,我们移除所有未激活的用户模型和收集其余各个用户的名字:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">where<span class="token punctuation">(</span></span><span class="token string">'active'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$names</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">reject<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">active</span> <span class="token operator">===</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">map<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">name</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 大部分的 Eloquent 集合会返回新的「Eloquent 集合」实例,但是 <code class=" language-php">pluck</code>, <code class=" language-php">keys</code>, <code class=" language-php">zip</code>, <code class=" language-php">collapse</code>, <code class=" language-php">flatten</code> 和 <code class=" language-php">flip</code> 方法会返回 <a href="/docs/5.4/collections">基础集合</a> 实例。</p><p>相应的,如果一个 <code class=" language-php">map</code> 操作返回一个不包含任何 Eloquent 模型的集合,那么它将会自动转换成基础集合。</p></blockquote><p><a name="available-methods"></a></p><h2><a href="#available-methods">可用的方法</a></h2><h3>集合对象</h3><p>所有 Eloquent 集合都继承了基础的 <a href="/docs/5.4/collections">Laravel 集合</a> 对象。因此,他们也继承了所有集合类提供的强大的方法:</p><style>#collection-method-list > p {
column-count: 3; -moz-column-count: 3; -webkit-column-count: 3;
column-gap: 2em; -moz-column-gap: 2em; -webkit-column-gap: 2em;
}
#collection-method-list a {
display: block;
}</style><div id="collection-method-list"><p><a href="/docs/5.4/collections#method-all">all</a> <a href="/docs/5.4/collections#method-avg">avg</a> <a href="/docs/5.4/collections#method-chunk">chunk</a> <a href="/docs/5.4/collections#method-collapse">collapse</a> <a href="/docs/5.4/collections#method-combine">combine</a> <a href="/docs/5.4/collections#method-contains">contains</a> <a href="/docs/5.4/collections#method-count">count</a> <a href="/docs/5.4/collections#method-diff">diff</a> <a href="/docs/5.4/collections#method-diffkeys">diffKeys</a> <a href="/docs/5.4/collections#method-each">each</a> <a href="/docs/5.4/collections#method-every">every</a> <a href="/docs/5.4/collections#method-except">except</a> <a href="/docs/5.4/collections#method-filter">filter</a> <a href="/docs/5.4/collections#method-first">first</a> <a href="/docs/5.4/collections#method-flatmap">flatMap</a> <a href="/docs/5.4/collections#method-flatten">flatten</a> <a href="/docs/5.4/collections#method-flip">flip</a> <a href="/docs/5.4/collections#method-forget">forget</a> <a href="/docs/5.4/collections#method-forpage">forPage</a> <a href="/docs/5.4/collections#method-get">get</a> <a href="/docs/5.4/collections#method-groupby">groupBy</a> <a href="/docs/5.4/collections#method-has">has</a> <a href="/docs/5.4/collections#method-implode">implode</a> <a href="/docs/5.4/collections#method-intersect">intersect</a> <a href="/docs/5.4/collections#method-isempty">isEmpty</a> <a href="/docs/5.4/collections#method-keyby">keyBy</a> <a href="/docs/5.4/collections#method-keys">keys</a> <a href="/docs/5.4/collections#method-last">last</a> <a href="/docs/5.4/collections#method-map">map</a> <a href="/docs/5.4/collections#method-max">max</a> <a href="/docs/5.4/collections#method-merge">merge</a> <a href="/docs/5.4/collections#method-min">min</a> <a href="/docs/5.4/collections#method-only">only</a> <a href="/docs/5.4/collections#method-pluck">pluck</a> <a href="/docs/5.4/collections#method-pop">pop</a> <a href="/docs/5.4/collections#method-prepend">prepend</a> <a href="/docs/5.4/collections#method-pull">pull</a> <a href="/docs/5.4/collections#method-push">push</a> <a href="/docs/5.4/collections#method-put">put</a> <a href="/docs/5.4/collections#method-random">random</a> <a href="/docs/5.4/collections#method-reduce">reduce</a> <a href="/docs/5.4/collections#method-reject">reject</a> <a href="/docs/5.4/collections#method-reverse">reverse</a> <a href="/docs/5.4/collections#method-search">search</a> <a href="/docs/5.4/collections#method-shift">shift</a> <a href="/docs/5.4/collections#method-shuffle">shuffle</a> <a href="/docs/5.4/collections#method-slice">slice</a> <a href="/docs/5.4/collections#method-sort">sort</a> <a href="/docs/5.4/collections#method-sortby">sortBy</a> <a href="/docs/5.4/collections#method-sortbydesc">sortByDesc</a> <a href="/docs/5.4/collections#method-splice">splice</a> <a href="/docs/5.4/collections#method-sum">sum</a> <a href="/docs/5.4/collections#method-take">take</a> <a href="/docs/5.4/collections#method-toarray">toArray</a> <a href="/docs/5.4/collections#method-tojson">toJson</a> <a href="/docs/5.4/collections#method-transform">transform</a> <a href="/docs/5.4/collections#method-union">union</a> <a href="/docs/5.4/collections#method-unique">unique</a> <a href="/docs/5.4/collections#method-values">values</a> <a href="/docs/5.4/collections#method-where">where</a> <a href="/docs/5.4/collections#method-wherestrict">whereStrict</a> <a href="/docs/5.4/collections#method-wherein">whereIn</a> <a href="/docs/5.4/collections#method-whereinloose">whereInLoose</a> <a href="/docs/5.4/collections#method-zip">zip</a></p></div><p><a name="custom-collections"></a></p><h2><a href="#custom-collections">自定义集合</a></h2><p>如果你需要使用一个自定义的 <code class=" language-php">Collection</code> 对象到自己的扩充方法上,则可以在模型中重写 <code class=" language-php">newCollection</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>CustomCollection</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 创建一个新的 Eloquent 集合实例对象。
*
* @param array $models
* @return \Illuminate\Database\Eloquent\Collection
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">newCollection<span class="token punctuation">(</span></span><span class="token keyword">array</span> <span class="token variable">$models</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">CustomCollection</span><span class="token punctuation">(</span><span class="token variable">$models</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>一旦你定义了 <code class=" language-php">newCollection</code> 方法,则可在任何 Eloquent 返回该模型的 <code class=" language-php">Collection</code> 实例时,接收到一个你的自定义集合的实例。如果你想要在应用程序的每个模型中使用自定义集合,则应该在所有的模型继承的模型基类中重写 <code class=" language-php">newCollection</code> 方法。</p><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://laravel-china.org/users/79">@skyverd</a></td><td><img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/79_1427370664.jpeg?imageView2/1/w/100/h/100"></td><td>翻译</td><td>全桟工程师,<a href="https://skyverd.com">时光博客</a></td></tr></tbody></table></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度