Search

Laravel Classes That Support Macros

November 21, 2016 —John Koster

The following table will list every framework class that implements the __call method directly. The table will also indicate if the class supports macros even though it implements __call. Please note that the following table does not take into account classes that implement the __callSatic method (such as the Illuminate\Support\Facades\Facade class).

Class Macroable?
Illuminate\Foundation\Support \Providers\RouteServiceProvider No
Illuminate\Support\ViewErrorBag No
Illuminate\Redis\Database No
Illuminate\Support\Manager No
Illuminate\Support\Fluent No
Illuminate\Cache\Repository Yes
Illuminate\Broadcasting \BroadcastManager No
Illuminate\Filesystem \FilesystemManager No
Illuminate\Http\RedirectResponse No
Illuminate\Queue\Capsule\Manager No
Illuminate\Routing\Controller No
Illuminate\Support \ServiceProvider No
Illuminate\Queue\QueueManager No
Illuminate\Pagination \AbstractPaginator No
Illuminate\Filesystem \FilesystemAdapter No
Illuminate\Cache\CacheManager No
Illuminate\Database \DatabaseManager No
Illuminate\Database \Eloquent\Relations\Relation No
Illuminate\Mail\Message No
Illuminate\View\View No
Illuminate\Database\Eloquent \Builder Yes See "Notes on Eloquent Builder"
Illuminate\Validation\Validator No
Illuminate\Database\Query \Builder No
Illuminate\Database\Eloquent \Model No

#Notes on Eloquent Builder

Even though the Illuminate\Database\Eloquent\Builder class supports macros, it does not use the Macroable trait. The Builder class exposes the following public methods for interacting with macros:

macro($name, Closure $callback)

: Adds a new macro to the Builder with the given $name and $callback.

getMacro($name)

: Returns the $callback (see macro($name Closure $callback) method) registered to the macro with the given $name.

The Builder class handles determining if macros exist internally and does not expose any public API for determining if a macro exists. This does not mean, however, that it cannot be determined if a macro exists for the Builder. Because the getMacro internally makes a call to the array_get helper function without specifying a value for $default, the getMacro function will return null if no matching macro is found.

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.