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 make multiple routes share the same URL structure or URL parameters

How to set up an alias to map one directory to another destination

What is WordPress default Apache .htaccess rules?

How to block an IP address in .htaccess

How to find the index of a value in an array in JS?

How to sort an array in javascript?

How to query mysql using PDO in PHP?

How to block multiple IP addresses in .htaccess

What does PEAR stand for?

Point all requests to one PHP file