An Introduction to Laravel Message Bags Nov 29, 2016
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.
An Introduction to Laravel View Error Bags Nov 29, 2016
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.
An Introduction to Laravel's Collection Public API Nov 29, 2016
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.
Laravel Collection Public API: all Nov 29, 2016
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.
Laravel Collection Public API: chunk Nov 29, 2016
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.
Laravel Collection Public API: collapse Nov 29, 2016
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.
Laravel Collection Public API: contains Nov 29, 2016
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.
Laravel Collection Public API: count Nov 29, 2016
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.
Laravel Collection Public API: diff Nov 29, 2016
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.
Laravel Collection Public API: diff_keys Nov 29, 2016
The diffKeys method is used to determine which items in a collection have keys that are not present in the keys of another collection. The method takes an array or another collection as its parameter. In the first example, the $differences variable will only contain the key price since it is the only key in the first collection that is not present in the second collection. In the second example, the $differences variable will only contain the key description since it is the only key in the second collection that is not present in the first collection.
Laravel Collection Public API: each Nov 29, 2016
The each method in Laravel's Collection class allows you to perform a $callback on each item in the collection. If the $callback function returns false, the loop will break. This method is similar to PHP's foreach construct. Even though the each method does not explicitly allow modifications to a collection's items, it is still possible to modify object properties within the collection due to how PHP treats objects as references. Keep in mind that the each method modifies the original collection and does not return a modified copy.
Laravel Collection Public API: filter Nov 29, 2016
The filter method is used to apply a filter to a collection in Laravel. You can provide your own truth tests using a callback function to determine if a value should be included in the final collection. By default, any value that evaluates to false will be removed from the collection. The method returns a modified copy of the original collection without modifying the original.
Laravel Collection Public API: first Nov 29, 2016
The first method in Laravel's Collection class is used to retrieve the first item in a collection. You can also pass a callback function to specify criteria for selecting the first item. If there are no items that match the criteria, you can provide a default value to be returned.
Laravel Collection Public API: flatMap Nov 29, 2016
The flatMap method in Laravel is similar to map, but it collapses the resulting collection. It is used to transform each item in the collection and then merge the results into a single collection. This can be demonstrated by comparing two code examples that achieve the same result: mapping each item to uppercase.