The Blog

Laravel 5

Laravel 5

Laravel 5 Collections: Randomizing Element Order With shuffle

John Koster
John Koster
April 22, 2018

The shuffle method in Laravel is used to randomly rearrange the items of an array collection. It returns a new collection instance, without modifying the original collection. The method takes an optional seed parameter to determine the random order.

Laravel 5

Laravel 5 Collections: Reducing a Collection Into a Collection of Key/Array-Value Pairs With mapToDictionary

John Koster
John Koster
April 22, 2018

The mapToDictionary method in PHP allows you to create a dictionary-like structure from a collection of data, where each key maps to one or more values. The method takes a callback function that defines the relationship between the keys and values. The resulting collection contains key/value pairs, with the values stored as arrays. This method does not modify the original array, but returns a new collection with the dictionary structure.

Laravel 5

Laravel 5 Collections: Reducing a Collection Into a Collection of Key/Collection-Value Pairs With mapToGroups

John Koster
John Koster
April 22, 2018

The mapToGroups method in PHP allows you to reduce a collection of data into key/value pairs. The resulting value for each key can be a single value or another collection. Compared to the mapToDictionary method, mapToGroups creates collections for each key's values instead of arrays. The mapToGroups method does not modify the original array and returns a new collection with a dictionary structure.

Laravel 5

Laravel 5 Collections: Reducing a Collection to One Element With reduce

John Koster
John Koster
April 22, 2018

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 element. The method also provides an optional initial value parameter. The reduce method is similar to PHP's array_reduce function. Examples are provided to demonstrate the basic usage of the reduce method, including getting the sum and difference of elements in a collection. An example is also given to show how to get the product of each item in a collection.

Laravel 5

Laravel 5 Collections: Reducing a Collection With User-Defined Keys With mapWithKeys

John Koster
John Koster
April 22, 2018

The mapWithKeys method in Laravel is used to associate keys and values based on a provided callback function. This method does not modify the original collection and returns a new collection. For example, you can use mapWithKeys to create a collection of products and their associated prices.

Laravel 5

Laravel 5 Collections: Reducing a Multi-Dimensional Collection to a Single Dimension Losing Keys With flatten

John Koster
John Koster
April 22, 2018

The flatten method in Laravel's Collection class can be used to create a new collection instance that represents a flattened version of the original collection's items. This method does not preserve any keys from the original collection. The method also works with deeply nested arrays, as demonstrated in the example.

Laravel 5

Laravel 5 Collections: Removing a Portion of a Collection With splice

John Koster
John Koster
April 22, 2018

The splice method in Laravel's Collection class allows you to remove a section of a collection and return the removed items as a new collection. It takes three parameters: $offset, $length, and $replacement. The $offset determines where to start removing items from the collection, and the $length controls how many items to remove. If provided, the $replacement parameter can be used to replace the removed items with new items.

Laravel 5

Laravel 5 Collections: Removing Collection Elements With forget

John Koster
John Koster
April 22, 2018

The forget method in Laravel's Collection class removes an item from the collection based on a given key. It modifies the original collection instance and returns a reference to it. A code example demonstrates how to use the forget method to remove an item from a collection, either by passing a string key or a numerical key.

Laravel 5

Laravel 5 Collections: Removing the First Element of a Collection With shift

John Koster
John Koster
April 22, 2018

The shift method in Laravel is used to remove the first item from a collection and return its value. It functions similar to PHP's array_shift function. An example use case is demonstrated where the method is used on a Collection instance to remove the first item.

Laravel 5

Laravel 5 Collections: Retrieving a Portion of a Collection With slice

John Koster
John Koster
April 22, 2018

The slice method in Laravel is used to return a portion of a collection starting at a given offset. The offset determines where to start when creating the new collection, and it can be positive or negative to start from the end of the original collection. The method returns a new instance of the Collection class without modifying the original collection. The slice method is similar to PHP's array_slice function. There is also an optional length parameter that allows developers to control the size of the returned Collection. Additionally, developers can choose to preserve the keys of the returned collection by passing true as the argument for the preserveKeys parameter.

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