The Blog

Laravel 5

Laravel 5 Collections: Filtering Collections With (Without Losing Filtered Rejections) partition

John Koster
John Koster
April 22, 2018

The partition method in PHP allows you to create two subsets of a collection based on a truth condition. Unlike the filter method, the partition method does not discard elements that failed the truth condition. Instead, it returns an array of two collections. This method is particularly useful when combined with PHP's list function. You can use the partition method with higher order messaging to access object instance properties and invoke collection methods.

Laravel 5

Laravel 5 Collections: Finding an Element Key Conditionally With search

John Koster
John Koster
April 22, 2018

The search method is used to search a collection for a given value or a callback function. If the value is found, the corresponding key is returned, otherwise it returns false. By default, the search is not strict and does not check for data types. However, you can enable strict mode to ensure data type matches. Additionally, you can provide a custom comparison function for more complex searches.

Laravel 5

Laravel 5 Collections: Getting the First Collection Element With first

John Koster
John Koster
April 22, 2018

The first method in Laravel's Collection class is used to retrieve the first item in a collection. It can also be used with a callback function to find the first item that matches certain criteria. If the collection is empty, it will return null, unless a default value is provided. The first method can also be used with higher order messaging to access object instance properties. Example use cases include retrieving the first item in a collection, handling empty collections, using a callback to filter the collection, and using higher order messaging to retrieve specific items.

Laravel 5

Laravel 5 Collections: Getting the Last Collection Element With last

John Koster
John Koster
April 22, 2018

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 given condition. You can pass a callback function to define the condition for matching. If the collection is empty, the method will return null, unless a default value is provided.

Laravel 5

Laravel 5 Collections: Getting the Number of Elements in a Collection With count

John Koster
John Koster
April 22, 2018

The count method in the Laravel framework returns the number of elements in the collection. It can be used to count the number of items in a collection and implements PHP's Countable interface. This method is synonymous with PHP's count function and returns an integer value.

Laravel 5

Laravel 5 Collections: Grouping Collection Elements With groupBy

John Koster
John Koster
April 22, 2018

The groupBy method in Laravel is used to group a collection based on a given value. The value can be specified as a simple string or a callback function. By default, keys are not preserved during grouping, but this can be changed by passing true as an argument. The method returns a new instance of the Collection class. Examples are provided to demonstrate how groupBy can be used with different parameters.

Laravel 5

Laravel 5 Collections: Mapping Collection Elements and Reducing to a Single Dimension With flatMap

John Koster
John Koster
April 22, 2018

The flatMap method is used to collapse a collection into a new modified collection. It does not modify the original collection, but returns a new one with the modified elements. You can use flatMap to apply higher order messaging and transform nested objects into arrays. This is useful when you need to extract specific values or modify the structure of your collection.

Laravel 5

Laravel 5 Collections: Merging Multiple Collection Values With merge

John Koster
John Koster
April 22, 2018

The merge method merges the given items with the items in the collection. It replaces any item in the original collection if a string key with the same value exists in the supplied items. If the item keys are numeric, the new items will be added to the end of the collection. The merge method can accept either an array or a Collection instance, and it returns a new instance of Collection without modifying the original collection. The behavior of the merge method is similar to PHP's array_merge function.

Laravel 5

Laravel 5 Collections: Modifying Collection Elements With transform

John Koster
John Koster
April 22, 2018

The transform method in Laravel's Collection class allows you to modify the original collection instance by applying a callback function to each item. Unlike the map method, it does not return a new Collection instance. This is useful for tasks like changing the case of strings or bridging method arguments for incompatible APIs. The example demonstrates how to use the transform method to convert all strings in a collection to uppercase.

Laravel 5

Laravel 5 Collections: Paginating Collections With forPage

John Koster
John Koster
April 22, 2018

Learn how to use the forPage method in Laravel to implement pagination over collections. This method allows you to specify the desired page and number of items per page. Get a new collection instance containing the items for the specified page. You can also achieve similar results using the chunk method to divide the collection into smaller collections. Use the get method to retrieve a specific page. Easy and efficient pagination for your flat-file driven content management systems.

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