The Blog

Laravel 5

Laravel 5

Laravel 5 Collections: Transforming Collection Element Keys With keyBy

John Koster
John Koster
April 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

Laravel 5 Collections: Transforming Collection Elements With map

John Koster
John Koster
April 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

Laravel 5 Facades

John Koster
John Koster
April 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

Laravel 5 Fluent API: Dynamic Data Containers and the Fluent Data Type

John Koster
John Koster
April 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

Laravel 5 Fluent API: The Public API

John Koster
John Koster
April 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

Laravel 5: Getting Specific Messages With get

John Koster
John Koster
April 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

Laravel 5 Macros: Call and Callstatic

John Koster
John Koster
April 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

Laravel 5 Macros: Creating Callback Macros

John Koster
John Koster
April 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.

Laravel 5

Laravel 5 Macros: Creating Class Based Macros with Mixins

John Koster
John Koster
April 21, 2018

Learn about the concept of "macro mixins" in Laravel 5.4, where you can define and group related macro methods and utility functions into a class, then inject them as macro methods to any class that uses the Illuminate\Support\Traits\Macroable trait.

Laravel 5

Laravel 5 Macros: Defining a Callback Function Macro With macro

John Koster
John Koster
April 21, 2018

Learn how to enhance your PHP classes with the macro method. This method adds a callback function macro to any class that uses the "Illuminate\Support\Traits\Macroable" trait. Check out an example of how to add a new method to a class and see how it works in action. Get the power to customize and extend your classes effortlessly.

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