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. hadoop2.2集群搭建问题只能启动一个datanode问题

    按照教程http://cn.soulmachine.me/blog/20140205/搭建总是出现如下问题: 2014-04-13 23:53:45,450 INFO org.apache.hadoo ...

  2. [Algorithms] Determine if a string is a palindrome

    A palindrome is a string that reads the same forward and backward, for example, radar, toot, and mad ...

  3. ubuntu下vi的使用

    ubuntu下vi的使用 ssh之后对于server的文件,我习惯用gedit,可是不好改动,于是就用vi. 1.vi的基本概念 基本上vi能够分为三种状态,各自是命令模式(command mode) ...

  4. cout 堆栈,operator&lt;&lt; 运算符重载输出问题

    在C++中cout的输出流其中,有一些问题非常easy出错,就比方以下这道简单程序.看似简单.但却是一个值得深思的问题~~ #include <iostream> using namesp ...

  5. TMS320F28335项目开发记录6_28335之cmd文件具体解释

    1.CMD文件的作用 CMD文件的作用就像仓库的货物摆放记录一样,为程序代码和数据分配指定的空间. 2.C语言生成的段 C语言生成的段大致分为两大类:初始化和未初始化,已初始化的段含有真正的指令和数据 ...

  6. Class 与 new的配合使用

    class Type{  // 定义新的类型Type /// ...... }; Type a;  Type b; // 像int a; int b;那样使用,  定义a和b为Type类型的变量 in ...

  7. placeholder 占位符

    placeholder 简介  |  TensorFlow https://tensorflow.google.cn/programmers_guide/low_level_intro 供给 目前来讲 ...

  8. POJ 2636:Electrical Outlets

    Electrical Outlets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9597   Accepted: 718 ...

  9. shell脚本执行错误:#!/bin/bash: No such file or directory

    执行.sh脚本时控制台报错 : #!/bin/bash: No such file or directory 解决办法: cat -A 文件路径 会发现第一行有问题 M-oM-;M-?#!/bin/b ...

  10. js几种escape()解码与unescape()编码

    js几种escape()解码与unescape()编码 www.111cn.net 编辑:kepeer 来源:转载 一篇js几种escape()解码与unescape()编码函数,同时我们也和它和服务 ...