Freek Van der Herten has a post to his site with a guide about debugging collections in Laravel applications. He quickly shows how to use the "macro" functionality to gather more insight into what's happening inside.
Lately I’ve been working a lot with collections in Laravel. If you’re not in the know: a collection is a sort of super charged array with a lot of powerful functions to transform the data inside it. The only thing I found a bit of a hassle is how to debug the various steps in a collection chain. Here’s how I improved the workflow.
He shows a simple collection setup with a chain of functions being applied to transform the data inside (ex: filter
, map
, sortBy
). He was able to get a bit more information by using the dd helper Laravel provides but it's not the most "clean" way to just wrap the collection on it. Instead he proposes he use of a "macro" to call the dd
function and keep it in the flow of the methods called on the collection (as ->dd()
).