Learn to use the Illuminate\Support\Collection class in Laravel to manipulate data using familiar array syntax. Access and iterate through the collection using key/value pairs. Retrieve values associated with known keys and work with the underlying array using the toArray method. Be aware that array-specific functions will not work directly on a collection, but can be used on the underlying array. Use methods like get to retrieve data from the collection with the option to return a default value. Create a new collection instance by passing an array of items to the constructor, or by using the make method or the collect helper function.
The Illuminate\Support\MessageBag class is a key/value storage system for storing different types of messages and specifying a format for returning them. It implements both the Illuminate\Contracts\Support\MessageProvider and Illuminate\Contracts\Support\MessageBag interfaces. The MessageProvider interface allows other classes to return MessageBag implementations. This article also provides a list of classes that extend, implement, or make use of the MessageProvider interface. Check out the rest of the articles in the series to learn more about the Laravel ErrorMessageBag component.
The Illuminate\Support\ViewErrorBag class is a container for error messages used to communicate with views and responses. When there are errors in the current request, an instance of ViewErrorBag is shared with views. The ViewErrorBag instance can be accessed using the errors name. The class provides several public methods for interacting with the error bags.
Learn about the public API of the Collection class in this section. With 64 methods, this extensive API allows you to retrieve values, transform arrays, implement pagination, and perform simple aggregates. You'll gain a deep understanding of this powerful class, excluding methods needed for implementing PHP's ArrayAccess and IteratorAggregateinterfaces, as well as the getCachingIterator method.
The all method in Laravel allows you to retrieve the underlying array that a collection is using to store its data. This method can be useful when you need to work with the raw array data instead of the collection object. The all method preserves any nested collections within the main collection, making it easy to access and manipulate the entire data structure. If you want to convert nested collections to arrays as well, you can use the toArray method instead.
The chunk method in Laravel allows you to break a large collection into smaller collections. The method takes two parameters: $size, which determines the size of each new collection, and $preserveKeys, which indicates whether the new collections should retain the original array keys. By default, the newly created collections will not have the original array keys, but you can change this behavior by setting $preserveKeys to true.
The collapse method is used to combine all the first-level items of a collection into a new, single collection. It does not recursively collapse inner arrays. When called, it returns an instance of the Collection class. The collapse method can be particularly useful when working with nested arrays or collections.
The contains method in Laravel's Collection class is used to check if a specific key or a key/value pair exists within the collection. It can be used with simple values, arrays, or even objects. The method can also take a function as an argument to perform complex truth tests. However, it's important to note that when the second argument is explicitly set to null, the method will always return true, regardless of the existence of the key or key/value pair.
The count method in Laravel's Collection class returns the total number of items in the collection. It is a straightforward method that returns an integer. Use the count method to easily determine the size of a collection without the need for a loop.
The diff method is used to find the items in the collection that are not present in another collection. It returns a new Collection instance. You can pass either an array or another Collection instance as the argument. Here are some examples of how to use diff with arrays and Collection instances.
Wrapping up Forte Phase 0 with the lexer and parser now in private alpha, and kicking off Phase 1 wi...
Read moreA Forte development update: the parser now supports backtracking, improvements to node metadata, ide...
Read moreWrapping up attribute parsing in Forte's HTML parser, from simple HTML attributes to complex, edge-c...
Read moreExploring how Forte's parser extensions can be used to handle complex Blade directives like nested s...
Read moreDigging into parsing Blade and HTML comments while building Forte's HTML parser for Laravel Blade.
Read moreThis week I’m tackling Forte's HTML parser - consolidating Blade, Flux, and Livewire components into...
Read more