Eloquent: Collections

Introduction

All multi-result sets returned by Eloquent are an instance of theIlluminate\Database\Eloquent\Collection object, including results retrieved via the get method or accessed via a relationship. The Eloquent collection object extends the Laravel base collection, so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models.

Of course, all collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:

$users = App\User::where('active', 1)->get();

foreach ($users as $user) {
echo $user->name;
}

However, collections are much more powerful than arrays and expose a variety of map / reduce operations using an intuitive interface. For example, let's remove all inactive models and gather the first name for each remaining user:

$users = App\User::where('active', 1)->get();

$names = $users->reject(function ($user) {
return $user->active === false;
})
->map(function ($user) {
return $user->name;
});

Available Methods

The Base Collection

All Eloquent collections extend the base Laravel collection object; therefore, they inherit all of the powerful methods provided by the base collection class:

[all](/docs/5.1/collections#method-all) [chunk](/docs/5.1/collections#method-chunk) [collapse](/docs/5.1/collections#method-collapse) [contains](/docs/5.1/collections#method-contains) [count](/docs/5.1/collections#method-count) [diff](/docs/5.1/collections#method-diff) [each](/docs/5.1/collections#method-each) [filter](/docs/5.1/collections#method-filter) [first](/docs/5.1/collections#method-first) [flatten](/docs/5.1/collections#method-flatten) [flip](/docs/5.1/collections#method-flip) [forget](/docs/5.1/collections#method-forget) [forPage](/docs/5.1/collections#method-forpage) [get](/docs/5.1/collections#method-get) [groupBy](/docs/5.1/collections#method-groupby) [has](/docs/5.1/collections#method-has) [implode](/docs/5.1/collections#method-implode) [intersect](/docs/5.1/collections#method-intersect) [isEmpty](/docs/5.1/collections#method-isempty) [keyBy](/docs/5.1/collections#method-keyby) [keys](/docs/5.1/collections#method-keys) [last](/docs/5.1/collections#method-last) [map](/docs/5.1/collections#method-map) [merge](/docs/5.1/collections#method-merge) [pluck](/docs/5.1/collections#method-pluck) [pop](/docs/5.1/collections#method-pop) [prepend](/docs/5.1/collections#method-prepend) [pull](/docs/5.1/collections#method-pull) [push](/docs/5.1/collections#method-push) [put](/docs/5.1/collections#method-put) [random](/docs/5.1/collections#method-random) [reduce](/docs/5.1/collections#method-reduce) [reject](/docs/5.1/collections#method-reject) [reverse](/docs/5.1/collections#method-reverse) [search](/docs/5.1/collections#method-search) [shift](/docs/5.1/collections#method-shift) [shuffle](/docs/5.1/collections#method-shuffle) [slice](/docs/5.1/collections#method-slice) [sort](/docs/5.1/collections#method-sort) [sortBy](/docs/5.1/collections#method-sortby) [sortByDesc](/docs/5.1/collections#method-sortbydesc) [splice](/docs/5.1/collections#method-splice) [sum](/docs/5.1/collections#method-sum) [take](/docs/5.1/collections#method-take) [toArray](/docs/5.1/collections#method-toarray) [toJson](/docs/5.1/collections#method-tojson) [transform](/docs/5.1/collections#method-transform) [unique](/docs/5.1/collections#method-unique) [values](/docs/5.1/collections#method-values) [where](/docs/5.1/collections#method-where) [whereLoose](/docs/5.1/collections#method-whereloose) [zip](/docs/5.1/collections#method-zip)

Custom Collections

If you need to use a custom Collection object with your own extension methods, you may override the newCollection method on your model:

<?php

namespace App;

use App\CustomCollection;
use Illuminate\Database\Eloquent\Model; class User extends Model
{
/**
* Create a new Eloquent Collection instance.
*
* @param array $models
* @return \Illuminate\Database\Eloquent\Collection
*/
public function newCollection(array $models = [])
{
return new CustomCollection($models);
}
}

Once you have defined a newCollection method, you will receive an instance of your custom collection anytime Eloquent returns a Collection instance of that model. If you would like to use a custom collection for every model in your application, you should override the newCollection method on a model base class that is extended by all of your models.

Laravel5.1学习笔记21 EloquentORM 集合的更多相关文章

  1. Laravel5.1学习笔记19 EloquentORM 入门

    Eloquent:入门 简介 定义模型(model) Eloquent 模型规范 取出多个模型 取出单个模型 / 集合 取出集合 插入更新模型  基本插入 基本更新 大批量赋值 删除模型 软删除 查询 ...

  2. Laravel5.1学习笔记20 EloquentORM 关系

    Eloquent: Relationships Introduction Defining Relationships One To One One To Many Many To Many Has ...

  3. 学习笔记 07 --- JUC集合

    学习笔记 07 --- JUC集合 在讲JUC集合之前我们先总结一下Java的集合框架,主要包含Collection集合和Map类.Collection集合又能够划分为LIst和Set. 1. Lis ...

  4. Ext.Net学习笔记21:Ext.Net FormPanel 字段验证(validation)

    Ext.Net学习笔记21:Ext.Net FormPanel 字段验证(validation) 作为表单,字段验证当然是不能少的,今天我们来一起看看Ext.Net FormPanel的字段验证功能. ...

  5. SQL反模式学习笔记21 SQL注入

    目标:编写SQL动态查询,防止SQL注入 通常所说的“SQL动态查询”是指将程序中的变量和基本SQL语句拼接成一个完整的查询语句. 反模式:将未经验证的输入作为代码执行 当向SQL查询的字符串中插入别 ...

  6. ArcGIS API for JavaScript 4.2学习笔记[21] 对3D场景上的3D要素进行点击查询【Query类学习】

    有人问我怎么这个系列没有写自己做的东西呢? 大哥大姐,这是"学习笔记"啊!当然主要以解读和笔记为主咯. 也有人找我要实例代码(不是示例),我表示AJS尚未成熟,现在数据编辑功能才简 ...

  7. [原创]java WEB学习笔记21:MVC案例完整实践(part 2)---DAO层设计

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  8. Laravel5.1学习笔记16 数据库2 查询构造器(这个不用看,不如用EloquentORM)

    Introduction Retrieving Results Aggregates Selects Joins Unions Where Clauses Advanced Where Clauses ...

  9. struts2视频学习笔记 21(输入校验的流程)

    课时21 输入校验的流程 1.类型转换器对请求参数执行类型转换,并把转换后的值赋给action中的属性. 2.如果在执行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,co ...

随机推荐

  1. Redis集群方案收集

    说明: 如果不考虑客户端分片去实现集群,那么市面上基本可以说就三种方案最成熟,它们分别如下所示: 系统 贡献者 是否官方Redis实现 编程语言 Twemproxy Twitter 是 C Redis ...

  2. Oldboy 基于Linux的C/C++自动化开发---MYSQL

    http://www.eimhe.com/forum.php?mod=viewthread&tid=142952#lastpost http://www.eimhe.com/thread-14 ...

  3. 【.Net 学习系列】-- Windows服务定时运行,判断当前时间是否在配置时间段内

    /// <summary> /// 判断程序是否在设置运行时间内 /// </summary> /// <param name="startTime" ...

  4. 原则 principles

    1.找到对的人来讨论问题. 2.把工作分配给对的人才能把事情做对. 3.

  5. Android清单文件具体解释(四) ---- backupAgent的使用方法

    在<application>节点中有一个很重要的属性,那就是backupAgent.这里我们将它单独列出来,从基本含义,使用方法及其相关属性等方面来具体介绍一下. 1.backupAgen ...

  6. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  7. 2016/1/21 练习 创建 接口interface 应用implements 类class 并实例化调用

    package testinterface; public interface ICpu { //电压 public boolean dianya(); //控制 public void kongzh ...

  8. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] B. Finding Team Member 排序

                                                                      B. Finding Team Member             ...

  9. python any and all function

    1 any 如果iterable object至少有一个元素是true的时候,返回为true.空的iterable object返回为false. 2 all 如果iterable object中的每 ...

  10. HDU 5783Divide the Sequence

    Divide the Sequence Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...