In Laravel

Laravel Collection Public API: combine

Nov 30, 2016

The combine method in Laravel is used to combine the keys of a collection with the values of another collection or array. It returns a new collection instance and does not modify the original one. However, both collections/arrays must have the same length for this method to work properly. An error will be thrown if they don't have an equal number of elements.
Laravel Collection Public API: every

Nov 30, 2016

The every method in Laravel can be used to retrieve a subset of a collection based on each item's distance from each other. You can define the distance between each item using the $step parameter, and specify the starting point using the optional $offset parameter. For example, you can retrieve all even numbers from a collection using $collection->every(2), or retrieve every 50th item from a collection using $collection->every(50).
Laravel Collection Public API: except

Nov 30, 2016

The except method in Laravel returns key/value pairs from a collection that are *not* in the specified keys array. You can use it to exclude sensitive data like passwords. By combining it with the toJson method, you can convert the resulting data to JSON format. This allows you to easily remove sensitive information before sending it to an end user.
Laravel Collection Public API: keyBy

Nov 30, 2016

The keyBy method in Laravel is used to create a new Collection instance where the keys of the new key/value pairs are determined by a callback function or a string. This method does not modify the original Collection instance and instead returns a new modified Collection instance. You can use a simple string value or a callback function to determine the keys of the new collection. For example, you can use the id as the key or even create a hash of the id to use as the key.
Laravel Collection Public API: only

Nov 30, 2016

The only method allows you to retrieve specific key/value pairs from a collection based on the keys supplied in the $keys array. For example, you can use it to get only the first_name and last_name values from a collection of users. After executing the only method, you will receive a new collection containing the desired key/value pairs.
Laravel Collection Public API: pluck

Nov 30, 2016

The pluck method in Laravel allows you to retrieve a list of values from a collection. It takes two parameters: $value and $key, indicating which property should become the value and the key in the resulting collection. You can use it to quickly extract specific properties from a collection.
Laravel Collection Public API: pop

Nov 30, 2016

The pop method in Laravel is used to retrieve and remove the last item from a collection. If the collection is empty, null is returned. The original collection is modified by this method.
Laravel Collection Public API: prepend

Nov 30, 2016

The prepend method adds a given value to the beginning of a collection. Optionally, you can specify a key for the new value when adding to an associative array. The method returns a reference to the original collection. Here is an example of using prepend to add an item to a collection: $collection->prepend('Select a shirt size'); When supplying an argument for the $key parameter, prepend can be used to add an item to an associative array. Here is an example: $collection->prepend('XL', 'size');
Laravel Collection Public API: pull

Nov 30, 2016

The pull method in Laravel's Collection class removes an item from the collection and returns its value. If the item does not exist, an optional default value can be provided. After executing the method, the original collection will be modified.
Laravel Collection Public API: push

Nov 30, 2016

Learn how to use the push method in PHP to add an item to the end of a collection. See code examples and understand the return value of this method. Be aware that the push method does not allow setting the key of the item being added to the collection.
Laravel Collection Public API: put

Nov 30, 2016

The put method is used to add a new item to a Collection object with a given key and value. It modifies the original collection instance and returns a reference to it. In the provided code example, a new item is added to the collection using the put method, resulting in the collection containing three items: 'first', 'second', and 'third', each with their respective values.
Laravel Collection Public API: random

Nov 30, 2016

The random method in Laravel's Collection class allows you to retrieve a random number of items from a collection. By default, it returns one random item, but you can specify a different number of items to be returned. If you request more items than are available in the collection, an exception will be thrown. You can use the random method by creating a new Collection instance and calling the method on it.
Laravel Collection Public API: reduce

Nov 30, 2016

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.
Laravel Collection Public API: reject

Nov 30, 2016

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.

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.