I found this post to be super helpful in working with collections: http://daylerees.com/codebright/eloquent-collections
Not sure if it'll get you what you need, but referring to it has saved my bacon on more than one occasion.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am having to use the L5 query builder to pull some data with some advanced relationships and some advanced joins.
Because I am using the query builder, my created_at timestamp is not being returned as a Carbon instance.
I was curious is anyone knows and easy way to manipulate a collections items? I know I can loop through them and make my adjustment. I was just curious if Laravel has a shorthand method to accomplish this?
I pretty much just want to convert "created_at" for all my collection items into a Carbon instance, so I am not messing with the structure or naming conventions of the collection.
Try this
$collection->map(function($item) {
$item->attributes['created_at'] = Carbon::whatever($item->created_at); // I don't remember the method
return $item;
});
Please or to participate in this conversation.