Hello artisans,
How are you doing? In this tutorial, I will discuss with you Laravel Macro Collection and Mixins. If you don’t know about this cool feature of laravel. Then you are in the right place. I will discuss everything in detail with examples. So here we go.

What is Laravel Macro?
Firstly, You have to know that what is a macro? In simple words, Laravel Macro is a way to add some missing or extra functionality, to Laravel’s internal component with a piece of code that doesn’t exist in the Laravel class. Well, To implement a Laravel Macro, Laravel provides a PHP trait called Macroable.
Macroable Laravel’s Classes
Well, The following Laravel’s classes allow for macros to be created by using the Illuminate\Support\Traits\Macroable trait. Here are some of the most commonly used classes to create macros.
- Request: Illuminate\Http\Request
- Response: Illuminate\Http\Response
- Collection: Illuminate\Support\Collection
- Str: Illuminate\Support\Str
- Router: Illuminate\Routing\Router
- UrlGenerator: Illuminate\Routing\UrlGenerator
- Cache: Illuminate\Cache\Repository
- Filesystem: Illuminate\Filesystem\Filesystem
- Arr: Illuminate\Support\Arr
- Rule: Illuminate\Validation\Rule
There are other classes and facades which use the Macroable
trait. Then You can find all the classes and facades in the codebase of Laravel.
Let’s see an example of Laravel Macro.
Well, To define a macro you can choose two options. If you want just two or three macro functions, then you can define inside the boot function App/Http/Providers/AppServiceProvider or make a dedicated service provider for macros. In this example, I will add a new couple of methods in Arr: Illuminate\Support\Arr and Str: Illuminate\Support\Str facades.
App\Providers\AppServiceProvider.php
//example 1
Str::macro('grettings', function (string $userName) {
return 'Hello ' . $userName;
});
//example 2
Arr::macro('multiplyWith', function ($arr, $n) {
return collect($arr)->map(function ($a) use ($n) {
return $a * $n;
});
});
In the first example, I have defined a simple macro function which is concatenation user name with “Hello” text. I know, definitely, you don’t like this function in your application. You can define your own logic whatever do you want.
Well, The second macro function I have defined that which is to receive an array and a number then return an array which is multiplied with the number.
Well, Now arise a question, How do you use this macro?
As it is, we use facades helper we can use macros function. I have defined a route for an example purpose you can use in your controller or others.
routes/web.php
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Route::get('macro', function () {
return Arr::multiplyWithN([10, 20, 30], 2); //[20,40,60]
return Str::grettings(Shahin); // Hello Shahin
});
Now I will show you how to use macros via mixins
Well, Mixins is nothing but simply a class. If you have a lot of macros on a specific facade then you can create a dedicated file for those functions.
Well, I have created a PHP class file in App/Mixins folder ArrayMixin.php
<?php
namespace App\Mixins;
use Illuminate\Support\Arr;
class ArrayMixin
{
public function multiplyWithN()
{
return function ($arr, $n) {
return collect($arr)->map(function ($a) use ($n) {
return $a * $n;
});
};
}
public function OtherFunction()
{
return function () {
return 10;
});
};
}
}
In this file, I have created two methods you can create whatever you want. It’s up to you. Well, then you have to register this file in AppServiceProvider.
public function boot(){
Arr::mixin(new ArrayMixin);
}
In conclusion, Then you can use those macros as usual facades methods.
Hope It will help You.
Read More: Load more Data in Laravel using Ajax/Axios