Laravel 5 Macros: Framework Classes with Dynamic Methods Available

April 21, 2018 —John Koster

The following table will list every framework class or trait that implements the __call method directly. The table will also indicate if the class or trait 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/Trait Macroable?
Illuminate\Auth\AuthManager No
Illuminate\Auth\Passwords\PasswordBrokerManager No
Illuminate\Broadcasting\BroadcastManager No
Illuminate\Cache\CacheManager No
Illuminate\Cache\Repository Yes
Illuminate\Database\DatabaseManager No
Illuminate\Database\Capsule\Manager No
Illuminate\Database\Eloquent\Builder Yes*
Illuminate\Database\Eloquent\Model No
Illuminate\Database\Eloquent\Relations\MorphTo No
Illuminate\Database\Eloquent\Relations\Relation Yes
Illuminate\Database\Query\Builder Yes
Illuminate\Filesystem\FilesystemAdapter No
Illuminate\Filesystem\FilesystemManager No
Illuminate\Foundation\Support\Providers\RouteServiceProvider No
Illuminate\Foundation\Testing\TestResponse Yes
Illuminate\Http\JsonResponse Yes
Illuminate\Http\RedirectResponse Yes
Illuminate\Http\Response Yes
Illuminate\Http\Resources\DelegatesToResources No
Illuminate\Mail\Mailable No
Illuminate\Mail\Message No
Illuminate\Pagination\AbstractPaginator No
Illuminate\Queue\QueueManager No
Illuminate\Queue\Capsule\Manager No
Illuminate\Redis\RedisManager No
Illuminate\Redis\Connections\Connection No
Illuminate\Redis\Connections\PhpRedisConnection No
Illuminate\Routing\Controller No
Illuminate\Routing\Router Yes
Illuminate\Routing\RouteRegistrar No
Illuminate\Support\Fluent No
Illuminate\Support\HigherOrderCollectionProxy No
Illuminate\Support\HigherOrderTapProxy No
Illuminate\Support\Manager No
Illuminate\Support\Optional Yes
Illuminate\Support\ViewErrorBag No
Illuminate\Validation\Validator No
Illuminate\View\View 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.