The Blog

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.

Laravel 5

Laravel 5 Macros: Determining if a Class Has a Macro Defined With hasMacro

John Koster
John Koster
April 21, 2018

Learn how to use the hasMacro method in Laravel. This method allows you to check if a class has a macro with a specific name defined. It returns true if the macro is found, otherwise it returns false. You can use this method to programmatically determine the existence of a macro in a class. Check out the examples to see how to implement and use the hasMacro method in your code.

Laravel 5

Laravel 5 Macros: Framework Classes with Dynamic Methods Available

John Koster
John Koster
April 21, 2018

This table lists framework classes and traits that directly implement the __call method, along with whether they support macros. Please note that classes implementing __callStatic are not included. The "Illuminate\Database\Eloquent\Builder" class supports macros, but does not use the Macroable trait. The class has methods for adding and retrieving macros, but no public API for determining their existence.

Laravel 5

Laravel 5 Macros: Injecting Class-Based Macros With mixin

John Koster
John Koster
April 21, 2018

The mixin static method in Laravel allows you to load the methods of a mixin class instance into a target class as macro methods. You can use the mixin method by passing a class instance as its argument. For example, you can define a string mixin class StringEncryptionMixins and add it to the Str class to provide encryption and decryption methods. Once loaded, you can use the mixin methods as static methods of the Str class.

Laravel 5

Laravel 5 Macros: Public API

John Koster
John Koster
April 21, 2018

This section explores the various methods available when using the Macroable trait in Laravel. Despite their limited number, these methods are extremely beneficial. Additional articles discuss specific methods in the macro's public API, such as defining a callback function macro, injecting class-based macros, determining if a class has a macro defined, and using call and callstatic methods.

Laravel 5

Laravel 5 Macros

John Koster
John Koster
April 21, 2018

Macros in Laravel are a way to add functionality to various classes at runtime, similar to C#'s extension methods. Through the "\Illuminate\Support\Traits\Macroable" trait, many default classes in Laravel can support macros. Some of these classes include Illuminate\Auth\RequestGuard, Illuminate\Database\Query\Builder, and Illuminate\Support\Collection. Using macros reduces code duplication and allows developers to achieve the desired result with fewer lines of code.

Laravel 5

Laravel 5 Message Bags: Adding Messages to the Message Bag With add

John Koster
John Koster
April 21, 2018

The add method in the MessageBag class allows you to add a new message identified by a given key and containing a specified value. It returns the current MessageBag instance. You can retrieve all messages in the form of an array using the getMessages method. In validation scenarios, the key is commonly the name of the form element being validated.

Laravel 5

Laravel 5 Message Bags: Checking if Messages Exist for a Specific Input Key With has

John Koster
John Koster
April 21, 2018

The has method can be used to determine if messages exist within a MessageBag instance for a given key. By calling the has method, it will return true if messages exist for the given key, and false if no messages exist. With this method, you can easily check if messages are present without having to iterate through the entire MessageBag to find a specific key.

Laravel 5

Laravel 5 Message Bags: Checking if There Are Any Messages With isEmpty

John Koster
John Koster
April 21, 2018

The isEmpty method in Laravel's MessageBag class is used to check if the message bag is empty. It returns a boolean value indicating whether the message bag has any messages or not. An example usage demonstrates how to create a new MessageBag instance, add messages to it, and then check if the message bag is empty or not.

Laravel 5

Laravel 5 Message Bags: Checking if There Are Any Messages With isNotEmpty

John Koster
John Koster
April 21, 2018

The isNotEmpty method in Laravel's MessageBag class checks if the instance contains any messages and returns a boolean value accordingly. It is simply an alias of the any method. For example, you can create a MessageBag instance, check if it has any messages using isNotEmpty, and get a boolean result.

Latest posts

That Escalated Quickly: All the New Things

The past six months or so have been incredibly busy. What started as a new article series about cust...

Read more
Troubleshooting a Statamic Illegal Offset Type Error when Viewing Collection Entries in the Control Panel

In this post I talk about how I resolved a mysterious illegal offset type error when viewing collect...

Read more
Creating Simple HTTP Redirect Routes from a Statamic Site

Generating a custom Laravel routes file from a Statamic website to redirect to a new domain.

Read more
Disabling Vite File Hashes

Disabling file hashes in Vite output can be accomplished by modifying your project's vite.config.js

Read more
Implementing a Custom Laravel Blade Precompiler for Volt and Livewire

Learn how to implement a custom component compiler for Laravel's Blade templating language in this p...

Read more
Creating a Hybrid Cache System for Statamic: Part Five

Part 5 of 6 covers implementing a cache namespace and labeling system, which we can use to target mu...

Read more