Eloquent: Collections
Introduction
All multi-result sets returned by Eloquent are instances of the Illuminate\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 that may be chained using an intuitive interface. For example, let's remove all inactive models and gather the first name for each remaining user:
$users = App\User::all();
$names = $users->reject(function ($user) {
return $user->active === false;
})
->map(function ($user) {
return $user->name;
});
{note} While most Eloquent collection methods return a new instance of an Eloquent collection, the pluck
, keys
, zip
, collapse
, flatten
and flip
methods return a base collection instance. Likewise, if a map
operation returns a collection that does not contain any Eloquent models, it will be automatically cast to a base collection.
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.6/collections#method-all)
[average](/docs/5.6/collections#method-average)
[avg](/docs/5.6/collections#method-avg)
[chunk](/docs/5.6/collections#method-chunk)
[collapse](/docs/5.6/collections#method-collapse)
[combine](/docs/5.6/collections#method-combine)
[concat](/docs/5.6/collections#method-concat)
[contains](/docs/5.6/collections#method-contains)
[containsStrict](/docs/5.6/collections#method-containsstrict)
[count](/docs/5.6/collections#method-count)
[crossJoin](/docs/5.6/collections#method-crossjoin)
[dd](/docs/5.6/collections#method-dd)
[diff](/docs/5.6/collections#method-diff)
[diffKeys](/docs/5.6/collections#method-diffkeys)
[dump](/docs/5.6/collections#method-dump)
[each](/docs/5.6/collections#method-each)
[eachSpread](/docs/5.6/collections#method-eachspread)
[every](/docs/5.6/collections#method-every)
[except](/docs/5.6/collections#method-except)
[filter](/docs/5.6/collections#method-filter)
[first](/docs/5.6/collections#method-first)
[flatMap](/docs/5.6/collections#method-flatmap)
[flatten](/docs/5.6/collections#method-flatten)
[flip](/docs/5.6/collections#method-flip)
[forget](/docs/5.6/collections#method-forget)
[forPage](/docs/5.6/collections#method-forpage)
[get](/docs/5.6/collections#method-get)
[groupBy](/docs/5.6/collections#method-groupby)
[has](/docs/5.6/collections#method-has)
[implode](/docs/5.6/collections#method-implode)
[intersect](/docs/5.6/collections#method-intersect)
[isEmpty](/docs/5.6/collections#method-isempty)
[isNotEmpty](/docs/5.6/collections#method-isnotempty)
[keyBy](/docs/5.6/collections#method-keyby)
[keys](/docs/5.6/collections#method-keys)
[last](/docs/5.6/collections#method-last)
[map](/docs/5.6/collections#method-map)
[mapInto](/docs/5.6/collections#method-mapinto)
[mapSpread](/docs/5.6/collections#method-mapspread)
[mapToGroups](/docs/5.6/collections#method-maptogroups)
[mapWithKeys](/docs/5.6/collections#method-mapwithkeys)
[max](/docs/5.6/collections#method-max)
[median](/docs/5.6/collections#method-median)
[merge](/docs/5.6/collections#method-merge)
[min](/docs/5.6/collections#method-min)
[mode](/docs/5.6/collections#method-mode)
[nth](/docs/5.6/collections#method-nth)
[only](/docs/5.6/collections#method-only)
[pad](/docs/5.6/collections#method-pad)
[partition](/docs/5.6/collections#method-partition)
[pipe](/docs/5.6/collections#method-pipe)
[pluck](/docs/5.6/collections#method-pluck)
[pop](/docs/5.6/collections#method-pop)
[prepend](/docs/5.6/collections#method-prepend)
[pull](/docs/5.6/collections#method-pull)
[push](/docs/5.6/collections#method-push)
[put](/docs/5.6/collections#method-put)
[random](/docs/5.6/collections#method-random)
[reduce](/docs/5.6/collections#method-reduce)
[reject](/docs/5.6/collections#method-reject)
[reverse](/docs/5.6/collections#method-reverse)
[search](/docs/5.6/collections#method-search)
[shift](/docs/5.6/collections#method-shift)
[shuffle](/docs/5.6/collections#method-shuffle)
[slice](/docs/5.6/collections#method-slice)
[sort](/docs/5.6/collections#method-sort)
[sortBy](/docs/5.6/collections#method-sortby)
[sortByDesc](/docs/5.6/collections#method-sortbydesc)
[splice](/docs/5.6/collections#method-splice)
[split](/docs/5.6/collections#method-split)
[sum](/docs/5.6/collections#method-sum)
[take](/docs/5.6/collections#method-take)
[tap](/docs/5.6/collections#method-tap)
[toArray](/docs/5.6/collections#method-toarray)
[toJson](/docs/5.6/collections#method-tojson)
[transform](/docs/5.6/collections#method-transform)
[union](/docs/5.6/collections#method-union)
[unique](/docs/5.6/collections#method-unique)
[uniqueStrict](/docs/5.6/collections#method-uniquestrict)
[unless](/docs/5.6/collections#method-unless)
[values](/docs/5.6/collections#method-values)
[when](/docs/5.6/collections#method-when)
[where](/docs/5.6/collections#method-where)
[whereStrict](/docs/5.6/collections#method-wherestrict)
[whereIn](/docs/5.6/collections#method-wherein)
[whereInStrict](/docs/5.6/collections#method-whereinstrict)
[whereNotIn](/docs/5.6/collections#method-wherenotin)
[whereNotInStrict](/docs/5.6/collections#method-wherenotinstrict)
[zip](/docs/5.6/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 base model class that is extended by all of your models.