The Blog

Laravel 5

Laravel 5

Laravel 5 Collections: Splitting a Collection Into Smaller Pieces With chunk

John Koster
John Koster
April 22, 2018

The chunk method in PHP is used to create smaller collections from larger collections. It takes two parameters: $size to control the size of each new collection, and $preserveKeys to indicate if the method should preserve array keys. By default, the method does not preserve array keys, but passing true as the second argument will include the original array keys in the resulting collections.

Laravel 5

Laravel 5 Collections Static API: Adding Methods to the Higher Order Message List With proxy

John Koster
John Koster
April 22, 2018

The proxy static method in Laravel's Collection class allows you to add additional methods to the list of methods that should be proxied for use with higher order messaging. These methods provide a cleaner syntax when invoking methods on collections of objects. For example, you can add the contains method to the list of proxied methods and then invoke it on a collection of objects using the -> syntax. Internally, the contains method will be executed on each object instance in the collection.

Laravel 5

Laravel 5 Collections Static API: Creating a Collection By Invoking a Callback a Given Number of Times With times

John Koster
John Koster
April 22, 2018

Learn how to use the times static method in Laravel's Collection class to easily create collections with a specified number of elements. The times method is similar to PHP's range function, but it provides additional functionality for collections. You can pass a $number argument to specify the number of elements and an optional $callback function to modify each element. Discover how to use the times method to populate a collection with a range of numbers or create a collection of Carbon date instances for the next few days. Be aware of potential duplicate dates when using the times method and consider storing the current date outside of the callback function.

Laravel 5

Laravel 5 Collections Static API: Creating a New Collection Instance From Arrays Using make

John Koster
John Koster
April 22, 2018

The make static method in Laravel's Collection class creates a new collection instance with the provided items. If the argument is already a collection instance, it will be returned as is. You can use this method to easily create collections in your code. For example, you can create a collection from an array of items using Collection::make($items), and it will return a new collection instance.

Laravel 5

Laravel 5 Collections Static API: Ensure a Value Is a Collection With wrap

John Koster
John Koster
April 22, 2018

The wrap static method in the Illuminate\Support\Collection class creates a collection from any value supplied to it. It is useful in situations where a collection instance is expected. For example, it can be used to ensure that function or method arguments are always a collection. It does not wrap a value if it is already a collection.

Laravel 5

Laravel 5 Collections Static API: Returning the Items of a Collection With unwrap

John Koster
John Koster
April 22, 2018

The unwrap method in Laravel's Collection class allows you to access the underlying items of a collection. If the value provided is already a collection, its underlying items will be returned. If the value is not a collection, the value will be returned as is. This method can be useful when you want to ensure that an argument supplied to a function is always an array.

Laravel 5

Laravel 5 Collections: Swapping Element Keys and Values With flip

John Koster
John Koster
April 22, 2018

The flip method in Laravel's Collection class returns a new instance of Collection with the keys and values swapped. It is similar to PHP's array_flip function. Using this method, you can easily reverse the order of items in a collection.

Laravel 5

Laravel 5 Collections: Testing All Collection Values With every

John Koster
John Koster
April 22, 2018

The every method in Laravel is used to determine if each element of a collection passes a given truth test. It can accept a callback function or an operator and comparison value. For example, it can be used to check if all input characters are alphabetic or if all users in a collection are verified. It supports various operators like equality, not equal, less than, greater than, etc. The every method can also be used with higher order messaging to access properties on objects.

Laravel 5

Laravel 5 Collections: The Public API

John Koster
John Koster
April 22, 2018

This section covers the public API of the Collection class in Laravel. It includes methods for retrieving collection elements, transforming the collection, pagination, and simple aggregate functions, among others. Some of the methods covered include retrieving elements with get, filtering elements with filter, grouping elements with groupBy, and sorting elements with sort.

Laravel 5

Laravel 5 Collections: The Static API

John Koster
John Koster
April 22, 2018

Learn how to work with the static API of the Collection class in Laravel 5. This API provides helpful functions for creating new collection instances, ensuring a value is a collection, returning items of a collection, creating a collection by invoking a callback, and adding methods to the higher order message list. Simplify your code with these useful static functions.

Latest posts

Where Things get Good: Moving on to Forte Development Phase 1

Wrapping up Forte Phase 0 with the lexer and parser now in private alpha, and kicking off Phase 1 wi...

Read more
Forte Update: Backtracking, Metadata, HTML Validation, and More

A Forte development update: the parser now supports backtracking, improvements to node metadata, ide...

Read more
Parsing HTML and Blade Attributes in Forte

Wrapping up attribute parsing in Forte's HTML parser, from simple HTML attributes to complex, edge-c...

Read more
Switch Statements and Parser Extensions in Forte

Exploring how Forte's parser extensions can be used to handle complex Blade directives like nested s...

Read more
Parsing Blade Comments in Forte

Digging into parsing Blade and HTML comments while building Forte's HTML parser for Laravel Blade.

Read more
Thoughts on HTML Elements and Blade Components in Forte

This week I’m tackling Forte's HTML parser - consolidating Blade, Flux, and Livewire components into...

Read more