Add Collection ids
macro
#12113
-
Really simple: https://github.com/craftcms/cms/blob/develop/src/base/ApplicationTrait.php#L1443 Collection::macro('one', function() {
/** @var Collection $this */
return $this->first(...func_get_args());
}); it would be awesome to have a Collection::macro('ids', function() {
/** @var Collection $this */
return ArrayHelper::getColumn($this->all(), 'id');
}); The less |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Made this possible for Craft 4.3 via #12140. Little more complicated than adding a new macro since we can’t assume every collection is going to contain items that have an Instead I added a new Also went ahead and gave |
Beta Was this translation helpful? Give feedback.
Made this possible for Craft 4.3 via #12140.
Little more complicated than adding a new macro since we can’t assume every collection is going to contain items that have an
id
key/property.Instead I added a new
craft\elements\ElementCollection
class, whichElementQuery::collect()
andElement::getEagerLoadedElements()
return instead ofCollection
.Also went ahead and gave
ElementCollection
awith()
method as well, so it’s now possible to eager-load nested elements after the fact, rather than having to specify everything before the element query is executed.