What are macros in Laravel?

Laravel: What are macros in Laravel?

Macros are a quite clever, but simple way to add methods to existing classes. Many of Laravel's built in classes, such as the Request facade, can have user defined macros added. Let me explain by a simple example.

To enable macros, you just have to add use \Illuminate\Support\Traits\Macroable; in the class to use the Macroable trait. You can add it to any class. Many of the built in classes that come with Laravel will already use this trait.

A simple example to explain and show how to use macros in Laravel

I think a simple example from scratch should explain things quickly:

  1. class SomeClass
  2. {
  3.     use \Illuminate\Support\Traits\Macroable;
  4.     protected $some_var='aaaaa';
  5. }
  7. SomeClass::macro("getSomeVar",function() {
  8.     return $this->some_var;
  9. });
  11. $a = new SomeClass;
  12. dump($a->getSomeVar());

Obviously, without the macro, there would be no way from anything outside of the class itself to access the $some_var property. But after adding the getSomeVar macro, you can access it.

This isn't a particularly useful macro - but it serves the point of showing how macros are set and how to use them.

A more real world example of using macros in Laravel

Here is an example that might be more useful in the real world.

Let's say you often want to return the first word, of the first element in a Collection object. This is trivial to do without a macro, but maybe you do it often enough that it makes sense to write a macro.

  1. // add the macro
  2. \Illuminate\Database\Eloquent\Collection::macro("firstWordOfFirstElement",function() {
  3.     return explode(" ",$this->first())[0];
  4. });
  6. // create a collection:
  7. $collection = collect("Apples are fruits","Computers are not food","Bananas are also fruits");
  9. // and use the macro...
  10. dd($collection->firstWordOfFirstElement()); //returns Apples

You would add the Request::macro(...) code in somewhere like your AppServiceProvider file.

webdevetc profile pic

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.


Comments and discussion about What are macros in Laravel?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Laravel language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

What are Template literals (Template strings) in JS?

What is Bubble sort, and how to write an implementation of it in PHP

How to order a Laravel hasMany relationship?

Why should you update APP_URL from from http://localhost?

Is multiple inheritance supported in PHP?

How to disable file hotlinking in Apache with .htaccess

Point all requests to one PHP file

How to round a number in JS?

What are macros in Laravel?

What does 'final class' and 'final function' (final method) mean in PHP?