The Blog

Laravel

Laravel

Laravel Collection Public API: pluck

John Koster
John Koster
November 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

Laravel Collection Public API: pop

John Koster
John Koster
November 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

Laravel Collection Public API: prepend

John Koster
John Koster
November 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

Laravel Collection Public API: pull

John Koster
John Koster
November 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

Laravel Collection Public API: push

John Koster
John Koster
November 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

Laravel Collection Public API: put

John Koster
John Koster
November 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

Laravel Collection Public API: random

John Koster
John Koster
November 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

Laravel Collection Public API: reduce

John Koster
John Koster
November 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

Laravel Collection Public API: reject

John Koster
John Koster
November 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

Laravel Collection Public API: reverse

John Koster
John Koster
November 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.

Latest posts

Where Things get Good: Moving on to Forte Development Phase 1

Wrapping up Forte Phase 0 with the lexer and parser now in private alpha, and kicking off Phase 1 wi...

Read more
Forte Update: Backtracking, Metadata, HTML Validation, and More

A Forte development update: the parser now supports backtracking, improvements to node metadata, ide...

Read more
Parsing HTML and Blade Attributes in Forte

Wrapping up attribute parsing in Forte's HTML parser, from simple HTML attributes to complex, edge-c...

Read more
Switch Statements and Parser Extensions in Forte

Exploring how Forte's parser extensions can be used to handle complex Blade directives like nested s...

Read more
Parsing Blade Comments in Forte

Digging into parsing Blade and HTML comments while building Forte's HTML parser for Laravel Blade.

Read more
Thoughts on HTML Elements and Blade Components in Forte

This week I’m tackling Forte's HTML parser - consolidating Blade, Flux, and Livewire components into...

Read more