Collections is a really powerful Laravel class that provides "a fluent, convenient wrapper for working with arrays of data". Don't just take our words for it, Adam Watham is working on a book called "Refactoring to Collections" and he also has a great screencast on using collections..
There are often times when we are working with Craft CMS and wish we had Collections support, so we decided to build this plugin!
- Download plugin release from this page
- Rename the download directory to
collections
and move into yourplugins
directory - Install Collections from the control panel settings
When working with data that is in an array...
$array = [
'eight', 'six', 'seven', 'five'
];
You can easily make the array a Collection using the following code:
$collection = craft()->collections->make($array);
Once you have your data in a Collection, you can instantly reap the rewards!
For example you can get the count of items in the array/Collection by simply calling:
$collection->count();
Wham-o! the count, in this case 4
, will be easily available!
What if you need to ensure that an array contains and item?
if ($collection->contains('six')) {
// returns true, then do something about it!
}
For the complete list of options when using Collections, see the list of available methods.
Venveo is a Digital Marketing Agency for Building Materials Companies in Blacksburg, VA. Learn more about us on our website.
The MIT License (MIT). Please see License File for more information.