Search

The blog - deep space.

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.
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.

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.