Eloquent: Collections
All multi-result sets returned by Eloquent are instances of the 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:
allavgcollapseconcatcontainsStrictcrossJoindiffdumpeachSpreadexceptfirstflattenforgetgethasintersectisNotEmptykeysmapmapSpreadmapWithKeysmedianminnthpadpipepoppullputreducereverseshiftslicesortBysplicesumtaptoJsonunionuniqueStrictvalueswherewhereInwhereNotInzip
Once you have defined a newCollection
method, you will receive an instance of your custom collection anytime Eloquent returns a 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.