In Laravel

Laravel Collection Public API: reverse

Nov 30, 2016

The reverse method reverses the order of items in a collection. It returns a new Collection instance. Numerical keys are not preserved, but non-numerical keys are. Check out the code example to understand how to use the reverse method.
Laravel Collection Public API: search

Nov 30, 2016

The search method is used to search for a given $value within the collection. When found, it returns the corresponding key, otherwise it returns false. You can also use the $strict parameter to check for exact data types in the collection. You can pass either a value or a callback function as the $value argument. Code examples are provided to illustrate the usage of the search method.
Laravel Collection Public API: shift

Nov 30, 2016

The shift method in PHP's Illuminate\Support\Collection class is used to remove the first item from the collection and return its value. It modifies the original collection instance. In the provided example, the shift method is used to remove the first item 'first' from the collection, resulting in the collection to only contain 'second' and 'third'.
Laravel Collection Public API: shuffle

Nov 30, 2016

Learn about the shuffle method in Laravel, which rearranges an array in a random distribution. This method returns a new collection without modifying the original instance. Use the provided code example to understand how shuffle can be implemented. Remember that the output will change each time the code is executed, so the given output is just an example.
Laravel Collection Public API: slice

Nov 30, 2016

The slice method in Laravel's Illuminate\Support\Collection class is used to retrieve a slice or portion of a collection. It takes an $offset parameter to determine where to start the slice. If the $offset is negative, the slice will start from the end of the collection. The slice method returns a new instance of Collection, and the original collection remains unchanged. The returned collection can be further controlled using the optional $length parameter, which determines the number of items in the slice. Additionally, developers can preserve the keys of the original collection by passing true as the third parameter in the slice method call.
Laravel Collection Public API: sort

Nov 30, 2016

The sort method is used to sort a collection in a specific order. If no custom sorting comparison function is provided, the collection will be sorted in a case-insensitive "natural order". A custom sorting comparison function can be supplied using the $callback parameter. The sort method returns a new instance of Collection and does not modify the original collection. The example demonstrates how to use the sort method with and without a custom sorting comparison function.
Laravel Collection Public API: sortBy

Nov 30, 2016

Learn how to use the sortBy method in Laravel's Collection class to sort collection items based on a callback function or by a specific key. Sort the collection in either ascending or descending order. The sortBy method does not modify the original collection but returns a new sorted instance. Find code examples to sort a collection by name or by the number of followers. Additionally, understand the different sorting options available, such as SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL, and SORT_FLAG_CASE. Get a comparison table showing the new collection order based on each sorting option.
Laravel Collection Public API: sortByDesc

Nov 30, 2016

The sortByDesc method in Laravel operates the same as the sortBy method, but without the $descending parameter. Internally, sortByDesc calls sortBy with true as the $descending parameter. The following examples demonstrate how to use sortBy and sortByDesc to sort a collection by the name attribute.
Laravel Collection Public API: splice

Nov 30, 2016

The splice method in Laravel is used to remove a portion of a collection and return the removed section. It takes three parameters: $offset, $length, and $replacement. The $offset parameter determines where to begin removing items from the collection. The method modifies the original Collection instance and returns the removed items as a new Collection instance. You can also specify a replacement array to replace the removed items in the collection.
Laravel Collection Public API: sum

Nov 30, 2016

The sum method is used to calculate the sum of all items in a collection. It takes an optional $callback parameter which can be used to specify which values should be added together. If the collection is empty, the method will return 0. In the given examples, the sum method is used to calculate the total number of speakers at Laracon EU for different years. The total can be counted by passing a callback function as the argument to the $callback parameter.
Laravel Collection Public API: take

Nov 30, 2016

The take method in Laravel is used to retrieve a specified number of items from a collection. It can retrieve items from the beginning or end of the collection depending on the provided $limit. The method returns a new Collection instance, leaving the original collection unchanged. This allows for easy manipulation and retrieval of data from collections in Laravel applications.
Laravel Collection Public API: toJson

Nov 30, 2016

The toJson method in Laravel allows you to easily convert a collection into its JSON representation. By default, it uses PHP's json_encode function and can accept optional $options for encoding. You can also format the JSON output by passing in the JSON_PRETTY_PRINT constant. If you have deeply nested data structures and need to specify a higher depth for encoding, you can accomplish this by using json_encode directly on the jsonSerialize method of the collection instance.
Laravel Collection Public API: transform

Nov 30, 2016

The transform method in Laravel is similar to the map method, but instead of creating a new Collection instance, it modifies the original one. This method is useful when you want to make changes to each item in the collection. In the provided code example, the transform method is used to convert all the strings in the collection to uppercase.
Laravel Collection Public API: unique

Nov 30, 2016

The unique method in Laravel allows you to get all the unique items in a collection. By default, it performs a case-sensitive check, but you can provide a $key option to further restrict which items are returned. The method returns a new instance of Collection with the unique items. You can also use a callback function as the $key to perform custom uniqueness checks, like case-insensitive checks.

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.