Laravel Collections Using macro() to extend collections


Example

The macro() function allows you to add new functionality to Illuminate\Support\Collection objects

Usage:

Collection::macro("macro_name", function ($parameters) {
    // Your macro
});

For example:

Collection::macro('uppercase', function () {
    return $this->map(function ($item) {
        return strtoupper($item);
    });
});

collect(["hello", "world"])->uppercase();

Result: ["HELLO", "WORLD"]