The blog

Laravel 5 Collections: Getting the Last Collection Element With last

Apr 22, 2018

The last method in Laravel's Collection class is used to retrieve the last item in a collection or the last item that matches a given condition. You can pass a callback function to define the condition for matching. If the collection is empty, the method will return null, unless a default value is provided.
Laravel 5 Collections: Getting the Number of Elements in a Collection With count

Apr 22, 2018

The count method in the Laravel framework returns the number of elements in the collection. It can be used to count the number of items in a collection and implements PHP's Countable interface. This method is synonymous with PHP's count function and returns an integer value.
Laravel 5 Collections: Grouping Collection Elements With groupBy

Apr 22, 2018

The groupBy method in Laravel is used to group a collection based on a given value. The value can be specified as a simple string or a callback function. By default, keys are not preserved during grouping, but this can be changed by passing true as an argument. The method returns a new instance of the Collection class. Examples are provided to demonstrate how groupBy can be used with different parameters.
Laravel 5 Collections: Mapping Collection Elements and Reducing to a Single Dimension With flatMap

Apr 22, 2018

The flatMap method is used to collapse a collection into a new modified collection. It does not modify the original collection, but returns a new one with the modified elements. You can use flatMap to apply higher order messaging and transform nested objects into arrays. This is useful when you need to extract specific values or modify the structure of your collection.
Laravel 5 Collections: Merging Multiple Collection Values With merge

Apr 22, 2018

The merge method merges the given items with the items in the collection. It replaces any item in the original collection if a string key with the same value exists in the supplied items. If the item keys are numeric, the new items will be added to the end of the collection. The merge method can accept either an array or a Collection instance, and it returns a new instance of Collection without modifying the original collection. The behavior of the merge method is similar to PHP's array_merge function.
Laravel 5 Collections: Modifying Collection Elements With transform

Apr 22, 2018

The transform method in Laravel's Collection class allows you to modify the original collection instance by applying a callback function to each item. Unlike the map method, it does not return a new Collection instance. This is useful for tasks like changing the case of strings or bridging method arguments for incompatible APIs. The example demonstrates how to use the transform method to convert all strings in a collection to uppercase.
Laravel 5 Collections: Paginating Collections With forPage

Apr 22, 2018

Learn how to use the forPage method in Laravel to implement pagination over collections. This method allows you to specify the desired page and number of items per page. Get a new collection instance containing the items for the specified page. You can also achieve similar results using the chunk method to divide the collection into smaller collections. Use the get method to retrieve a specific page. Easy and efficient pagination for your flat-file driven content management systems.
Laravel 5 Collections: Pair the Values of a Collection With the Values of Other Collections With zip

Apr 22, 2018

The zip method in Laravel's Collection class is used to merge the values of two collections. It creates a new collection with elements placed at the corresponding index from the first collection. The zip method can accept multiple arguments of any data type that can be converted to an array. If the arguments have different lengths, null is used for missing values or extra values are added as a new collection.
Laravel 5 Collections: Randomizing Element Order With shuffle

Apr 22, 2018

The shuffle method in Laravel is used to randomly rearrange the items of an array collection. It returns a new collection instance, without modifying the original collection. The method takes an optional seed parameter to determine the random order.
Laravel 5 Collections: Reducing a Collection Into a Collection of Key/Array-Value Pairs With mapToDictionary

Apr 22, 2018

The mapToDictionary method in PHP allows you to create a dictionary-like structure from a collection of data, where each key maps to one or more values. The method takes a callback function that defines the relationship between the keys and values. The resulting collection contains key/value pairs, with the values stored as arrays. This method does not modify the original array, but returns a new collection with the dictionary structure.
Laravel 5 Collections: Reducing a Collection Into a Collection of Key/Collection-Value Pairs With mapToGroups

Apr 22, 2018

The mapToGroups method in PHP allows you to reduce a collection of data into key/value pairs. The resulting value for each key can be a single value or another collection. Compared to the mapToDictionary method, mapToGroups creates collections for each key's values instead of arrays. The mapToGroups method does not modify the original array and returns a new collection with a dictionary structure.
Laravel 5 Collections: Reducing a Collection to One Element With reduce

Apr 22, 2018

The reduce method is used to reduce a collection into a single item. It iterates over the collection and applies a callback function on each element. The method also provides an optional initial value parameter. The reduce method is similar to PHP's array_reduce function. Examples are provided to demonstrate the basic usage of the reduce method, including getting the sum and difference of elements in a collection. An example is also given to show how to get the product of each item in a collection.
Laravel 5 Collections: Reducing a Collection With User-Defined Keys With mapWithKeys

Apr 22, 2018

The mapWithKeys method in Laravel is used to associate keys and values based on a provided callback function. This method does not modify the original collection and returns a new collection. For example, you can use mapWithKeys to create a collection of products and their associated prices.
Laravel 5 Collections: Reducing a Multi-Dimensional Collection to a Single Dimension Losing Keys With flatten

Apr 22, 2018

The flatten method in Laravel's Collection class can be used to create a new collection instance that represents a flattened version of the original collection's items. This method does not preserve any keys from the original collection. The method also works with deeply nested arrays, as demonstrated in the example.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.