The Blog

Laravel

Laravel

Laravel Collection Public API: except

John Koster
John Koster
November 30, 2016

The except method in Laravel returns key/value pairs from a collection that are *not* in the specified keys array. You can use it to exclude sensitive data like passwords. By combining it with the toJson method, you can convert the resulting data to JSON format. This allows you to easily remove sensitive information before sending it to an end user.

Laravel

Laravel Collection Public API: keyBy

John Koster
John Koster
November 30, 2016

The keyBy method in Laravel is used to create a new Collection instance where the keys of the new key/value pairs are determined by a callback function or a string. This method does not modify the original Collection instance and instead returns a new modified Collection instance. You can use a simple string value or a callback function to determine the keys of the new collection. For example, you can use the id as the key or even create a hash of the id to use as the key.

Laravel

Laravel Collection Public API: only

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

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.

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