The blog

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

Apr 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 Collections Static API: Returning the Items of a Collection With unwrap

Apr 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 Collections: Swapping Element Keys and Values With flip

Apr 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 Collections: Testing All Collection Values With every

Apr 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 Collections: The Public API

Apr 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 Collections: The Static API

Apr 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.
Laravel 5 Collections: Transforming Collection Element Keys With keyBy

Apr 22, 2018

The keyBy method in Laravel is used to create a new collection instance where the keys of the new key/value pairs are determined by a callback function or a string value. This method does not change the original collection instance and will return a new modified instance. In the provided example, the keyBy method is used to create a new collection where each item is keyed by its id value. It can also be used with a callback function to perform more complex keying operations, such as generating a hash of each item's id.
Laravel 5 Collections: Transforming Collection Elements With map

Apr 22, 2018

The map method in Laravel's Collection class allows you to apply a callback function to each item in the collection and create a new collection with the modified items. The original collection remains unchanged. The callback function should accept the item and its key as its arguments. You can also use the map method in conjunction with higher order messages to transform objects into arrays. For example, you can transform an array of product objects into an array of their respective arrays using the map method.
Laravel 5 Facades

Apr 21, 2018

Facades in Laravel provide a simplified interface to access components. They act as proxies to underlying subsystems and are located within the "Illuminate\\Support\\Facades" namespace. Facades can be used to retrieve data from the cache, and are imported at the top of the source file. When creating a custom facade, the facade class must extend Laravel's abstract Facade class and specify the component to be resolved from the service container. Creating a facade alias is optional and can be done by adding a new entry to the aliases array in the config/app.php file.
Laravel 5 Fluent API: Dynamic Data Containers and the Fluent Data Type

Apr 21, 2018

Learn how to use the Illuminate\Support\Fluent class to easily work with data in PHP. The Fluent class allows you to access and retrieve data from arrays and objects without worrying about errors. You can assume certain properties exist in the data without causing fatal errors. The class simplifies code and makes it easier to work with data from external sources or multiple development teams.
Laravel 5 Fluent API: The Public API

Apr 21, 2018

Learn how to use the public methods of the Fluent class, such as get, getAttributes, toArray, jsonSerialize, and toJson. These methods allow you to retrieve values, get key/value data associations, convert the object to an array or JSON, and more. See examples of how to use these methods and understand their signatures and functionalities. Also, learn about the behavior of objects containing closures when used with Fluent, and how to handle deeply nested data structures when calling the toJson method.
Laravel 5: Getting Specific Messages With get

Apr 21, 2018

The get method retrieves all messages for a given key with a specified format. The format is optional and defaults to null, which uses the format stored in the MessageBag instance. Developers can customize the results by specifying their own format. The method returns an array containing all of the formatted messages. Here's an example use case where messages are retrieved for the key first with the format "The message was: :message". The resulting array will only contain messages related to the first key.
Laravel 5 Macros: Call and Callstatic

Apr 21, 2018

The Macroable trait in PHP implements the magic __call method to intercept calls to methods that don't exist in the current class. It checks if a macro with the given method name is found and evaluates it. If no macro is found, it throws an exception. Similarly, the trait also implements the magic __callStatic method for intercepting calls to methods in static context. However, it is currently not possible to use the Macroable trait in a class that requires returning a value from either __call or __callStatic without modifying the parent class's methods to accommodate the macro features.
Laravel 5 Macros: Creating Callback Macros

Apr 21, 2018

Learn how to create macros using callback functions in Laravel. Callback function macros can be added to classes by calling the macro method on the macroable class and supplying the callback function. You can create powerful macro functions that add functionality to the framework by accessing private and protected members of the class. Check out examples of creating a rot13 macro for the Illuminate\Support\Str class and accessing the protected static $studlyCache member. Macros can be called from both static and instance contexts, offering flexibility and enhancing the capabilities of your code.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.