The Blog

Laravel

Laravel Collection Public API: isEmpty

John Koster
John Koster
November 29, 2016

Learn how to use the isEmpty method in Laravel's Collection class to check if a collection is empty or not. By using this method, true will be returned if the collection is empty, otherwise false.

Laravel

Laravel Collection Public API: jsonSerialize

John Koster
John Koster
November 29, 2016

The jsonSerialize method is used internally by the toArray method in PHP's JsonSerializable interface. This interface allows developers to define custom representations of a class when using the json_encode function. In the provided example, a new collection is created and the jsonSerialize method is used to obtain an array of its values. The resulting array would contain the values 'first', 'second', and 'third'.

Laravel

Laravel Collection Public API: keys

John Koster
John Koster
November 29, 2016

The keys method in Laravel retrieves all the keys of the items in a collection. It returns a new Collection instance. You can use this method to get the keys of both indexed and associative arrays.

Laravel

Laravel Collection Public API: last

John Koster
John Koster
November 29, 2016

The last method in Laravel's Collection class is used to retrieve the last item in a collection or the last item that matches a specified condition. It takes an optional callback function ($callback) to perform the truth test, and a default value argument ($default) to return when no matching items are found. If the collection is empty, null is returned, unless a default value is provided.

Laravel

Laravel Collection Public API: lists

John Koster
John Koster
November 29, 2016

The lists method in Laravel is an alias of the pluck method. It retrieves a list of values from a collection. You can specify a property to become the value and another property to become the key in the resulting collection. There is also an example provided demonstrating the usage of the lists method to retrieve product names and versions from a collection. Another example shows how to get a new collection with the version as the value and the product name as the key.

Laravel

Laravel Collection Public API: map

John Koster
John Koster
November 29, 2016

The map method in Laravel allows you to apply a given callback to each item in a collection, creating a new collection with the modified items. The provided callback should accept the item and its key as arguments. In the example, the map method is used to transform the case of each string in a collection to upper-case, resulting in a new collection with the modified values. The original collection remains unchanged.

Laravel

Laravel Collection Public API: max

John Koster
John Koster
November 29, 2016

Learn how to use the max() method in Laravel to retrieve the maximum value of a given key. By default, the key is set to null, similar to PHP's max() function. Follow the provided code examples to see how to use the max() method without specifying a key or when specifying a key.

Laravel

Laravel Collection Public API: merge

John Koster
John Koster
November 29, 2016

The merge method in Laravel allows you to combine the items in a collection with other items. If a string key with the same value already exists in the collection, it will be replaced. Numeric keys in the new items will be added to the end of the collection. You can pass an array or another Collection instance to the merge method, and it will return a new instance of Collection.

Laravel

Laravel Collection Public API: min

John Koster
John Koster
November 29, 2016

The min method in Laravel is used to retrieve the minimum value of a given key. If no key is specified, it will return the minimum value of the collection. You can also use the min method to retrieve the minimum value of a specific key in a collection of arrays. For example, $min will contain the value 0 when the min method is called without specifying a key, and $min will contain the value 5.0 when called with the key 'version'.

Laravel

Laravel Collection Public API: toArray

John Koster
John Koster
November 29, 2016

The toArray method in Laravel converts a collection or an object instance that implements the Arrayable interface into an array. It returns the underlying array that the collection instance is using. This method is useful when you need to work with the data in array format rather than a collection.

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