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

What are macros in Laravel?

PHP Date commonly used formats

How to remove .php from the end of URLs with .htaccess

How to get the last character of a string in JS?

How to get the type of a variable in JS?

How to force gzip compression in Apache with .htaccess

Force trailing slash (redirect if no trailing slash exists) in .htaccess

How to select all rows for a certain month (or day, year or time), using Eloquent

What are the predefined variables in PHP?

What are Apache MPMs?