Laravel5.1学习笔记21 EloquentORM 集合

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.

时间: 2024-08-18 18:01:21

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

Laravel5.1学习笔记19 EloquentORM 入门

Eloquent:入门 简介 定义模型(model) Eloquent Model Conventions Retrieving Multiple Models Retrieving Single Models / Aggregates Retrieving Aggregates Inserting & Updating Models Basic Inserts Basic Updates Mass Assignment Deleting Models Soft Deleting Queryin

Laravel5.1学习笔记20 EloquentORM 关系

Eloquent: Relationships Introduction Defining Relationships One To One One To Many Many To Many Has Many Through Polymorphic Relations Many To Many Polymorphic Relations Querying Relations Eager Loading Constraining Eager Loads Lazy Eager Loading Ins

学习笔记 07 --- JUC集合

学习笔记 07 --- JUC集合 在讲JUC集合之前我们先总结一下Java的集合框架,主要包含Collection集合和Map类.Collection集合又能够划分为LIst和Set. 1. List的实现类主要有: LinkedList, ArrayList, Vector, Stack. (01) LinkedList是双向链表实现的双端队列:它不是线程安全的.仅仅适用于单线程. (02) ArrayList是数组实现的队列,它是一个动态数组.它也不是线程安全的,仅仅适用于单线程. (03

swift学习笔记之二——集合

//=========================== //2014/7/21 17:27 swift集合 //=========================== swift提供了两种集合类型,arrays和dictionaryies,两种集合都是可变的,可以在集合声明后对其进行新增.删除和修改操作. 1.array 数组的定义与java数组相同,但swift的数组提供了更灵活的创建方式和操作方式. 数组创建和初始化方式: var array1: Array<T> = [val1,va

python基础教程_学习笔记21:文件和素材

文件和素材 打开文件 open函数用来打开文件,语法如下: open([name[,mode[,buffering]]) open函数使用一个文件名作为唯一的强制参数,然后返回一个文件对象.模式(mode)和缓冲(buffering)参数都是可选的. >>> f=open(r'D:\software(x86)\Python27\README.txt') 如果文件不存在,则出现错误: >>> f=open(r'D:\software(x86)\Python27\READM

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

Ext.Net学习笔记21:Ext.Net FormPanel 字段验证(validation) 作为表单,字段验证当然是不能少的,今天我们来一起看看Ext.Net FormPanel的字段验证功能. 约束功能 为了防止用户输入不合法的数据,我们可以使用约束功能来限制用户的输入,例如TextField的 MinLength/MaxLength,NumberField的MinValue/MaxValue,DateField的MinDate /MaxDate等属性,它们可以将用户输入的值限定在一个合

多线程编程学习笔记——使用并发集合(三)

接上文 多线程编程学习笔记——使用并发集合(一) 接上文 多线程编程学习笔记——使用并发集合(二) 四.   使用ConcurrentBag创建一个可扩展的爬虫 本示例在多个独立的即可生产任务又可消费任务的工作者间如何扩展工作量. 1.程序代码如下. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Sy

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

本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 ---------------------------------

struts2视频学习笔记 21

课时21 输入校验的流程 1.类型转换器对请求参数执行类型转换,并把转换后的值赋给action中的属性. 2.如果在执行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,conversionError拦截器将异常信息添加到fieldErrors里.不管类型转换是否出现异常,都会进入第3步.(类型转换失败和校验失败都会跳转至input视图) 3.系统通过反射技术先调用action中的validateXxx()方法,Xxx为方法名. 4.再调用action中的validat