Use Laravel Collections in Craft CMS Plugins
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.., (*1)
There are often times when we are working with Craft CMS and wish we had Collections support, so we decided to build this plugin!, (*2)
Requirements
Installation and Setup
- Download plugin release from this page
- Rename the download directory to
collections
and move into your plugins
directory
- Install Collections from the control panel settings
Example Usage
When working with data that is in an array..., (*3)
$array = [
'eight', 'six', 'seven', 'five'
];
You can easily make the array a Collection using the following code:, (*4)
$collection = craft()->collections->make($array);
Once you have your data in a Collection, you can instantly reap the rewards!, (*5)
For example you can get the count of items in the array/Collection by simply calling:, (*6)
$collection->count();
, (*7)
Wham-o! the count, in this case 4
, will be easily available!, (*8)
What if you need to ensure that an array contains and item?, (*9)
if ($collection->contains('six')) {
// returns true, then do something about it!
}
, (*10)
For the complete list of options when using Collections, see the list of available methods., (*11)
Credits
About Venveo
Venveo is a Digital Marketing Agency for Building Materials Companies in Blacksburg, VA. Learn more about us on our website., (*12)
License
The MIT License (MIT). Please see License File for more information., (*13)