The Blog

Laravel

Laravel Collection Public API: count

John Koster
John Koster
November 29, 2016

The count method in Laravel's Collection class returns the total number of items in the collection. It is a straightforward method that returns an integer. Use the count method to easily determine the size of a collection without the need for a loop.

Laravel

Laravel Collection Public API: diff

John Koster
John Koster
November 29, 2016

The diff method is used to find the items in the collection that are not present in another collection. It returns a new Collection instance. You can pass either an array or another Collection instance as the argument. Here are some examples of how to use diff with arrays and Collection instances.

Laravel

Laravel Collection Public API: diff_keys

John Koster
John Koster
November 29, 2016

The diffKeys method is used to determine which items in a collection have keys that are not present in the keys of another collection. The method takes an array or another collection as its parameter. In the first example, the $differences variable will only contain the key price since it is the only key in the first collection that is not present in the second collection. In the second example, the $differences variable will only contain the key description since it is the only key in the second collection that is not present in the first collection.

Laravel

Laravel Collection Public API: each

John Koster
John Koster
November 29, 2016

The each method in Laravel's Collection class allows you to perform a $callback on each item in the collection. If the $callback function returns false, the loop will break. This method is similar to PHP's foreach construct. Even though the each method does not explicitly allow modifications to a collection's items, it is still possible to modify object properties within the collection due to how PHP treats objects as references. Keep in mind that the each method modifies the original collection and does not return a modified copy.

Laravel

Laravel Collection Public API: filter

John Koster
John Koster
November 29, 2016

The filter method is used to apply a filter to a collection in Laravel. You can provide your own truth tests using a callback function to determine if a value should be included in the final collection. By default, any value that evaluates to false will be removed from the collection. The method returns a modified copy of the original collection without modifying the original.

Laravel

Laravel Collection Public API: first

John Koster
John Koster
November 29, 2016

The first method in Laravel's Collection class is used to retrieve the first item in a collection. You can also pass a callback function to specify criteria for selecting the first item. If there are no items that match the criteria, you can provide a default value to be returned.

Laravel

Laravel Collection Public API: flatMap

John Koster
John Koster
November 29, 2016

The flatMap method in Laravel is similar to map, but it collapses the resulting collection. It is used to transform each item in the collection and then merge the results into a single collection. This can be demonstrated by comparing two code examples that achieve the same result: mapping each item to uppercase.

Laravel

Laravel Collection Public API: flatten

John Koster
John Koster
November 29, 2016

The flatten method in Laravel's Collection class allows you to transform a nested collection into a single-dimensional collection. This method does not preserve any keys from the original collection, even for deeply nested arrays.

Laravel

Laravel Collection Public API: flip

John Koster
John Koster
November 29, 2016

The flip method in Laravel allows you to exchange keys with their corresponding values in a Collection. This can be useful when you need to perform operations based on the values rather than the keys. In the provided example, the $flippedCollection variable will contain a new instance of the Collection class where the original keys have been swapped with their corresponding values.

Laravel

Laravel Collection Public API: forget

John Koster
John Koster
November 29, 2016

The forget method in Laravel can be used to remove an item from a collection based on a given key or numerical index. It modifies the collection instance it was called on. You can use the forget method to easily remove items from a collection and maintain the collection's structure and integrity.

Latest posts

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
Laracon, Blade and What's Next

Recapping Laracon in Denver with the Statamic team, this post introduces Forte - a new Blade extensi...

Read more