The Blog

Laravel

Laravel

Laravel Collection Public API: contains

John Koster
John Koster
November 29, 2016

The contains method in Laravel's Collection class is used to check if a specific key or a key/value pair exists within the collection. It can be used with simple values, arrays, or even objects. The method can also take a function as an argument to perform complex truth tests. However, it's important to note that when the second argument is explicitly set to null, the method will always return true, regardless of the existence of the key or key/value pair.

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.

Latest posts

That Escalated Quickly: All the New Things

The past six months or so have been incredibly busy. What started as a new article series about cust...

Read more
Troubleshooting a Statamic Illegal Offset Type Error when Viewing Collection Entries in the Control Panel

In this post I talk about how I resolved a mysterious illegal offset type error when viewing collect...

Read more
Creating Simple HTTP Redirect Routes from a Statamic Site

Generating a custom Laravel routes file from a Statamic website to redirect to a new domain.

Read more
Disabling Vite File Hashes

Disabling file hashes in Vite output can be accomplished by modifying your project's vite.config.js

Read more
Implementing a Custom Laravel Blade Precompiler for Volt and Livewire

Learn how to implement a custom component compiler for Laravel's Blade templating language in this p...

Read more
Creating a Hybrid Cache System for Statamic: Part Five

Part 5 of 6 covers implementing a cache namespace and labeling system, which we can use to target mu...

Read more