I often use Laravel Collections to refactor loops, nested functions and callbacks.
It also provides so many nice helper methods.
Would you consider using it internally for iterable objects/arrays?
Check out Refactoring to Collections for ideas.
+270000000 for this.
Venveo has a Collections plugin for Craft 2 plugin, and I'm currently working on a similar package for Craft 3. I'd be thrilled if collections made it to core, and double-thrilled if we could do it in such a way that the helper methods are "Craft aware" (i.e. some special syntax for using Craft service methods as callables).
If you want to use collections in Craft 3 you can just require this package https://github.com/tightenco/collect and use it wherever you want. A plug-in is not really necessary for Craft 3.
Sounds like advantage is in having several paths being pursued, and that either a plugin or @Rias500 's solution keeps the ground free for such, especially into futures...
I've been a fan of collections long enough, while also suspect that keeping them at a little distance from new developer entry to the Craft arena can probably be helpful to conain overload potential -- use array skills until the person is ready for something better.
@Rias500 This issue is about including it in the core, and all the benefits that would bring. I disagree about it not being "necessary".
If it's isn't included in the core, that makes it (much?) more cumbersome to use. Especially the possibility of using macro'd collection methods in Twig would be a huge benefit to cleaning up more advanced templates.
@narration-sd As far as I understand it, Collection implements ArrayAccess, which allow you to treat it as a array, should you wish to do so.
I assume that means people can still use their array skills.
I see the potential problems with adding this, though, so I'd leave it up to P&T to weight the pros and cons.
I'm all for the pros, though.
For now, I've created a plugin that makes it accessible in Twig: Collections for Craft 3
Really excellen, Fred -- you did it, and anyone can use this now.
I wouldn't worry at al labout its being accessible, as a _partner_ to core; look at how people seek out things like Supertable.
Where they appreciate it, now it's available. For those confronting all the real changes in Yii2/Craft 3, collections abilities can wait until they're ready to appreciate what these might do for their case, no?
Experimental yiisoft/yii2-collection exists.
https://github.com/yiisoft/yii2-collection/blob/master/Collection.php
There is now an official illuminate/collections package. Requires PHP ^7.3 so not going to work for Craft 3, but will put this on the 4.0 milestone.
Most helpful comment
There is now an official illuminate/collections package. Requires PHP ^7.3 so not going to work for Craft 3, but will put this on the 4.0 milestone.