By John Koster
The Collection
class exposes a generous public API, consisting of many public methods that allow you to manipulate and transform the collections internal elements. The Collection
API exposes methods for retrieving values from a collection, transforming the underlying array, pagination and simple aggregate functions, along with many others. This section will cover most of the methods in the public API, excluding those methods required to implement PHP's ArrayAccess
and IteratorAggregate
interfaces, as well as the getCachingIterator
method.
#Collections: The Public API
- Laravel 5 Collections: Retrieving Collection Elements With all
- Laravel 5 Collections: Convert a Collection to a Native PHP Array With toArray
- Laravel 5 Collections: Splitting a Collection Into Smaller Pieces With chunk
- Laravel 5 Collections: Converting a Multi-Dimensional Collection Into a Single Dimension With collapse
- Laravel 5 Collections: Determining If a Collection Contains an Element With contains
- Laravel 5 Collections: Determining if a Collection Contains an Element Using Strict Comparison With containsStrict
- Laravel 5 Collections: Getting the Number of Elements in a Collection With count
- Laravel 5 Collections: Determine Which Collection Elements are Not Present in the Provided Collections With diff
- Laravel 5 Collections: Returning Collection Elements That Are Not Present in the Specified Collections With diffAssoc
- Laravel 5 Collections: Retrieving Collection Elements Whose Keys Are Not Present in the Provided Keys With diffKeys
- Laravel 5 Collections: Executing a Function On All Collection Elements With each
- Laravel 5 Collections: Filtering Collection Elements With filter
- Laravel 5 Collections: Getting the First Collection Element With first
- Laravel 5 Collections: Reducing a Multi-Dimensional Collection to a Single Dimension Losing Keys With flatten
- Laravel 5 Collections: Swapping Element Keys and Values With flip
- Laravel 5 Collections: Paginating Collections With forPage
- Laravel 5 Collections: Removing Collection Elements With forget
- Laravel 5 Collections: Retrieving Collection Elements With get
- Laravel 5 Collections: Grouping Collection Elements With groupBy
- Laravel 5 Collections: Determining If a Collection Has An Element With has
- Laravel 5 Collections: Combining Collection Elements Into a String With implode
- Laravel 5 Collections: Filtering Collections Based On Value Presence With intersect
- Laravel 5 Collections: Checking If a Collection Contains Items With isEmpty
- Laravel 5 Collections: Converting a Collection Into Something JSON Serializable With jsonSerialize
- Laravel 5 Collections: Retrieving the Collection Element's Keys With keys
- Laravel 5 Collections: Getting the Last Collection Element With last
- Laravel 5 Collections: Transforming Collection Elements With map
- Laravel 5 Collections: Mapping Collection Elements and Reducing to a Single Dimension With flatMap
- Laravel 5 Collections: Retrieving the Maximum Value of a Collection With max
- Laravel 5 Collections: Merging Multiple Collection Values With merge
- Laravel 5 Collections: Calculating the Minimum Value of a Collection With min
- Laravel 5 Collections: Retrieving Collection Element Values With pluck
- Laravel 5 Collections: Retrieving and Removing the Last Collection Element With pop
- Laravel 5 Collections: Adding an Element to the Beginning of a Collection With prepend
- Laravel 5 Collections: Retrieving and Removing an Element From a Collection With pull
- Laravel 5 Collections: Adding an Element to the End of a Collection With push
- Laravel 5 Collections: Adding New Elements to a Collection With put
- Laravel 5 Collections: Retrieving Random Collection Elements With random
- Laravel 5 Collections: Reducing a Collection to One Element With reduce
- Laravel 5 Collections: Conditionally Removing Elements From a Collection With reject
- Laravel 5 Collections: Reversing the Order of Collection Elements With reverse
- Laravel 5 Collections: Finding an Element Key Conditionally With search
- Laravel 5 Collections: Removing the First Element of a Collection With shift
- Laravel 5: Randomizing Element Order With shuffle
- Laravel 5 Collections: Retrieving a Portion of a Collection With slice
- Laravel 5 Collections: Sorting a Collection With sort
- Laravel 5 Collections: Sorting a Collection Based on a Key Value With sortBy
- Laravel 5 Collections: Sort a Collection in Descending Order With sortByDesc
- Laravel 5 Collections: Removing a Portion of a Collection With splice
- Laravel 5 Collections: Calculating the Average Value of a Collection With avg
- Laravel 5 Collections: Calculating the Average Value of a Collection With average
- Laravel 5 Collections: Calculating the Sum of a Collection With sum
- Laravel 5 Collections: Retrieving a Specified Number of Items From a Collection With take
- Laravel 5 Collections: Converting a Collection's Contents to JSON With toJson
- Laravel 5 Collections: Converting a Collection to JSON With PHP's Magic __toString Method
- Laravel 5 Collections: Modifying Collection Elements With transform
- Laravel 5 Collections: Retrieving the Distinct Values of a Collection With unique
- Laravel 5 Collections: Retrieving Only the Values of a Collection's Elements With values
- Laravel 5 Collections: Filtering Collection Elements With where
- Laravel 5 Collections: Pair the Values of a Collection With the Values of Other Collections With zip
- Laravel 5 Collections: Combining the Keys of a Collection With the Values of Another Collection With combine
- Laravel 5 Collections: Testing All Collection Values With every
- Laravel 5 Collections: Excluding Collection Elements With except
- Laravel 5 Collections: Excluding Collection Elements With except
- Laravel 5 Collections: Transforming Collection Element Keys With keyBy
- Laravel 5 Collections: Filtering a Collection Based On Key Presence With whereIn
- Laravel 5 Collections: Creating Combination of Elements With crossJoin
- Laravel 5 Collections: Dumping the Collection Contents and Stopping Script Execution for Debugging With dd
- Laravel 5 Collections: Dumping the Collection Contents and Continuing Script Execution for Debugging With dump
- Laravel 5 Collections: Executing a Function on Collection Elements With eachSpread
- Laravel 5 Collections: Filtering Collections Based on Key Presence With intersectByKeys
- Laravel 5 Collections: Checking If a Collection Contains Items With isNotEmpty
- Laravel 5 Collections: Convert Collection Elements Into Object Instances With mapInto
- Laravel 5 Collections: Executing a Function on Collection Elements and Values With mapSpread
- Laravel 5 Collections: Reducing a Collection Into a Collection of Key/Array-Value Pairs With mapToDictionary
- Laravel 5 Collections: Reducing a Collection Into a Collection of Key/Collection-Value Pairs With mapToGroups
- Laravel 5 Collections: Reducing a Collection With User-Defined Keys With mapWithKeys
- Laravel 5 Collections: Retrieving Collection Elements At a Specific Interval With nth
- Laravel 5 Collections: Ensuring a Collection Always Contains a Specified Number of Elements With pad
- Laravel 5 Collections: Filtering Collections With (Without Losing Filtered Rejections) partition
- Laravel 5 Collections: Executing a Function on a Collection and Returning the Function Results With pipe
- Laravel 5 Collections: Breaking a Collection Into a Specified Number of Groups With split
- Laravel 5 Collections: Adding Values to a Collection With union
- Laravel 5 Collections: Retrieving the Distinct Values of a Collection Using Strict Comparison Operators With uniqueStrict
- Laravel 5 Collections: Conditionally Executing a Callback On a Collection's Elements With unless
- Laravel 5 Collections: Conditionally Executing a Callback On a Collection's Elements With when
- Laravel 5 Collections: Filtering a Collection Based On Key Presence Using Strict Comparison Operators With whereInStrict
- Laravel 5 Collections: Filtering a Collection Based On Key Presence Using Strict Comparison Operators With whereNotInStrict
- Laravel 5 Collections: Filtering a Collection Based On Key Presence With whereNotIn
- Laravel 5 Collections: Filtering Collection Elements Using Strict Comparison Operators With whereStrict
- Laravel 5 Collections: Calculating the Median Value of a Collection With median
- Laravel 5 Collections: Determining the Most Common Value in a Collection With mode
- Laravel 5 Collections: Combining the Values of Multiple Traversable Sources With concat
- Laravel 5: Execute a Callback on a Given Value While Chaining the Original Value With tap
- Laravel 5 Collections: Creating a Copy of a Collection With toBase
∎