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.
Laravel5.1学习笔记21 EloquentORM 集合的更多相关文章
- Laravel5.1学习笔记19 EloquentORM 入门
Eloquent:入门 简介 定义模型(model) Eloquent 模型规范 取出多个模型 取出单个模型 / 集合 取出集合 插入更新模型 基本插入 基本更新 大批量赋值 删除模型 软删除 查询 ...
- Laravel5.1学习笔记20 EloquentORM 关系
Eloquent: Relationships Introduction Defining Relationships One To One One To Many Many To Many Has ...
- 学习笔记 07 --- JUC集合
学习笔记 07 --- JUC集合 在讲JUC集合之前我们先总结一下Java的集合框架,主要包含Collection集合和Map类.Collection集合又能够划分为LIst和Set. 1. Lis ...
- Ext.Net学习笔记21:Ext.Net FormPanel 字段验证(validation)
Ext.Net学习笔记21:Ext.Net FormPanel 字段验证(validation) 作为表单,字段验证当然是不能少的,今天我们来一起看看Ext.Net FormPanel的字段验证功能. ...
- SQL反模式学习笔记21 SQL注入
目标:编写SQL动态查询,防止SQL注入 通常所说的“SQL动态查询”是指将程序中的变量和基本SQL语句拼接成一个完整的查询语句. 反模式:将未经验证的输入作为代码执行 当向SQL查询的字符串中插入别 ...
- ArcGIS API for JavaScript 4.2学习笔记[21] 对3D场景上的3D要素进行点击查询【Query类学习】
有人问我怎么这个系列没有写自己做的东西呢? 大哥大姐,这是"学习笔记"啊!当然主要以解读和笔记为主咯. 也有人找我要实例代码(不是示例),我表示AJS尚未成熟,现在数据编辑功能才简 ...
- [原创]java WEB学习笔记21:MVC案例完整实践(part 2)---DAO层设计
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- Laravel5.1学习笔记16 数据库2 查询构造器(这个不用看,不如用EloquentORM)
Introduction Retrieving Results Aggregates Selects Joins Unions Where Clauses Advanced Where Clauses ...
- struts2视频学习笔记 21(输入校验的流程)
课时21 输入校验的流程 1.类型转换器对请求参数执行类型转换,并把转换后的值赋给action中的属性. 2.如果在执行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,co ...
随机推荐
- 18.9.22 noip模拟赛
此题为找规律.期望100 实际100 #include<cstdio> #include<cstring> #include<iostream> #include& ...
- http转https
1.先用jdk生成证书 先跳转到jdk的bin目录下:E:\Program Files\Java\jdk1.8.0_91\bin>keytool -genkey -alias tomcat -k ...
- 程序猿是如何解决SQLServer占CPU100%的--马非码
http://www.cnblogs.com/marvin/p/ASolutionForSQLServerCauseHighCPU.html
- windows安装docker
主要參考:http://docs.docker.com/installation/windows/ [1]安装完毕后同意后可能会报错: error in run: Failed to start ma ...
- Base Conversion In PHP and javascript
http://www.exploringbinary.com/base-conversion-in-php-using-built-in-functions/ http://www.binarycon ...
- 对json的爱恨情仇
本文回想了对json的爱恨情仇. C++有风险,使用需慎重. 本文相关代码在:http://download.csdn.net/detail/baihacker/7862785 当中的測试数据不在里面 ...
- Objective-C 内存管理retain和release
OC使用引用计数来管理内存,每个继承NSObject的对象,内部都维护了一个引用计数器retainCount.当对象创建时(调用alloc或者new)引用计数器会+1, 手动调用retain()方法能 ...
- 2016/2/18 html 图片热点,网页划区,拼接,表单
①图片热点 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 显示 ②网页划区 在一个网页里,规划出一个区域用来展示另一个网页的内容. ③网页拼接 在一个网络页面内,规划 ...
- 谈谈C++私有继承
很多C++程序猿从来没用过私有继承来设计他的类.的确,假设是本该用私有继承的地方却用了公有继承.对程序的功能的实现并无影响. 但这样的误用是一种错位的描写叙述.会引起阅读者的误解,甚至会引起类的使用者 ...
- Linux/Android——usb触摸屏驱动 - usbtouchscreen (一)【转】
本文转载自:http://blog.csdn.net/jscese/article/details/41827495 最近需要往TV上装一个触摸屏设备,现在比较常见的就是使用usb接口的触摸框,适用于 ...