Eloquent:collections
- Introduction
- Available Methods
- Custom Collections
Introduction
All Multi-result sets returned by eloquent is an instance Illuminate\Database\Eloquent\Collection
of the object, including results retrieved via the get
m Ethod or accessed via a relationship. The eloquent collection object extends the Laravel base collection, so it naturally inherits dozens of methods used to flu ently work with the underlying array of eloquent models.
Of course, all collections also serve as iterators, allowing-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 is 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 methodsthe 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:
<?phpnamespace 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 has defined a newCollection
method, you'll receive an instance of your custom collection anytime eloquent returns a instance of that model. If you would a custom collection for every model in your application, you should override the newCollection
method on A model base class is extended by all of your models.
Laravel5.1 Study Notes Eloquentorm Collection