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. }
  6.  
  7. SomeClass::macro("getSomeVar",function() {
  8.     return $this->some_var;
  9. });
  10.  
  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. });
  5.  
  6. // create a collection:
  7. $collection = collect("Apples are fruits","Computers are not food","Bananas are also fruits");
  8.  
  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
webdevetc

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.



More...


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

How to create random numbers in JS?

What are route patterns in Laravel?

How to show (or log) all SQL queries executed by Laravel

What are generators in PHP, and how do they compare to arrays?

How to merge/concat two arrays together in JS?

How to round a number in JS?

How to check if a (blade) view file exists

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

How to return an item in Eloquent, or throw a ModelNotFoundException exception if it isn't found?

How does PHP compare objects?