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.
最新文章
- iOS-NSDate
- C++实现大数据乘法
- [GeoServer]Openlayers简单调用
- Ensemble Approaches分类技术
- 现在不使用ZeroClipboard我们也能实现复制功能(转)
- Delphi2010生成GB2312字库乱码问题
- 从一张图开始,谈一谈.NET Core和前后端技术的演进之路
- C#总结小程序
- Bootstrap 总结
- h5 文件下载
- jQuery选择器总结 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法
- 《Android进阶之光》--RxJava
- uvm设计分析——callback
- FastDFS:Java客户都实现文件的上传、下载、修改、删除
- android -------- android studio 中设置创建类时的说明信息(包含 作者 ,创建时间,注释说明等)
- Metabase在Windows下的开发环境配置
- 普通用户无法su到root用户
- Java中反射和Unsafe破坏单例设计模式
- 深入理解JAVA虚拟机阅读笔记2——垃圾回收
- Rookey.Frame之DAL工厂
热门文章
- fastjson过滤器简单记录
- Mac 系统引导过程概述 &; BootCamp 的秘密
- 如何启动/关闭weblogic
- Windows 2008 R2 SP1部署WSUS 3.0 SP2
- Hadoop HDFS NFS GateWay部署深入具体解释
- NoSQL数据库概览及其与SQL语法的比較
- HDU-3295-An interesting mobile game(BFS+DFS)
- 阿里云CentOS7.3搭建多用户私有git服务器(从安装git开始)
- PH_Pooled Featrues Classification MIREX 2011 Submission
- 8148和8127中的ezsdk和dvrsdk