Sevalla is the all-in-one PaaS for your web projects. Host and deploy your applications, databases, object storage, and static sites. Enjoy advanced deployment pipelines, a complete database studio, instant preview apps, and one-click templates. The pricing is simple: no hidden fees, no seat-based pricing, and you pay only for what you use. Get real human support from developers.

Get started now with a $50 credit at Sevalla.com.

Using collection macros in Laravel

Link –

Laravel 5.2 provides some nice additions to the framework. One handy feature that I don't see listed in the release notes is that Collection now is macroable. Using it's macro function you can easily extend Illuminate\Support\Collection with your own custom functions.

Take a look at this piece of code to uppercase every string in a collection.

$uppercaseWords = collect(['code', 'ferengi'])->map(function($word)  {
   return strtoupper($word);
});

That's good code, but image you need to uppercase a lot of collections. Typing the same closure will get very tiresome. Let's improve this with a macro.

use Illuminate\Support\Collection;

Collection::macro('uppercase', function() {

    return collect($this->items)->map(function($word) {
        return strtoupper($word);
    });

});

You could create a service provider to load up these macro's. Now that the macro is defined let's uppercase collections like there's no tomorrow:

$uppercaseWords = collect(['code', 'ferengi'])->uppercase();
$moreUppercaseWords = collect(['love', 'the', 'facade'])->uppercase();
$evenMoreUppercaseWords = collect(['activerecord', 'forever'])->uppercase();

You could be thinking "Why should I use a macro? I can easily to this with a regular function.". Consider this piece of code.

function uppercase($collection) {
...
}

$uppercaseWords = uppercase(collect(['halo','five']));

It works, but you have to encapsulate the collection with your function. The last executed function is put first, which is confusing. With macro's you can still chain functions and greatly improve readability.

//lots of functions
function4(function3(function2(function1(collect(['jack','cheats'])))));

//lots of macros
collect(['i', 'want', 'to', 'live', 'in', 'a', 'desert'])
  ->function1()
  ->function2()
  ->function3()
  ->function4();

Sure, the examples use in this post were a bit contrived, but I hope you see that collection macro's can be very handy.

EDIT: it seems that collection macro's were introduced in Laravel 5.1.25 a month ago.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.