Subscribe to PHP Freaks RSS

Pineco.de: Extending Laravel’s Collection With Macros

syndicated from www.phpdeveloper.org on July 20, 2018

On the Pineco.de blog they've posted a new tutorial showing how to extend the Collection functionality of Laravel with the help of macros.

Collections are fantastic features of the framework. With the functionality that the collection offers, we can easily handle any need that a dataset requires. But still, in some cases, we may extend with custom functionality.

The post starts with a more general look at what collection macros are and a few code snippets of them in action. To illustrate how to extend them, they create a "some" macro that takes in an anonymous function containing some kind of logic to check the key/value passed to it and return a boolean. The post finishes up by linking to the macro documentation and another package that implements a wide range of additional macros (like "extract", "paginate", "tail" and "transpose").