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 item. The reduce method accepts an optional initial value that will become the initial carry value of the callback function. If no items are in the collection, the initial value is returned. In the provided examples, the reduce method is used to find the sum and difference of a collection, as well as the product of each item. The importance of setting the initial value to 1 is also explained.
The reject method creates a new Collection by excluding items that do not pass a specified truth test. It takes a single parameter, $callback, which is a function that determines whether an item should be removed. If the $callback returns true, the item is excluded. For example, you can use reject to filter out names that do not start with the letter 'a' by passing a callback that checks for this condition.
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.
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.
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'.
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.
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.
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.
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.
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.
The past six months or so have been incredibly busy. What started as a new article series about cust...
Read moreIn this post I talk about how I resolved a mysterious illegal offset type error when viewing collect...
Read moreGenerating a custom Laravel routes file from a Statamic website to redirect to a new domain.
Read moreDisabling file hashes in Vite output can be accomplished by modifying your project's vite.config.js
Read moreLearn how to implement a custom component compiler for Laravel's Blade templating language in this p...
Read morePart 5 of 6 covers implementing a cache namespace and labeling system, which we can use to target mu...
Read more