Collections are souped-up arrays. It’s purpose is to work more fluently with large datasets and keep your code more readable. It’s very much modelled after Laravel’s base collection.
Collections are available through Cuisine’s wrapper system. You can include it’s class like this:
Using the facade, you can easily create collection from any array, like so:
$collection = Collection::make([ 1, 2, 3 ]);
A collection uses a return value to determine what to return. In a default collection this takes just two forms:
$items = $collection->toArray()->all();
This example turns the collection into an array and then gives all items back with the method.
$json = $collection->toJson()->all();
This example sets the returnValue to json and then returns all items as a (you guessed it) json string.
Returns all items, in the set return value (defaults to array)
$allItems = $collection->all();
Returns a single item out of the collection. If the key can’t be found, it returns the default value passed in the second parameter.
$singleItem = $collection->get( $key, 'defaultValue' );
Returns the first value of a collection:
$firstValue = $collection->first();
Returns a boolean on wether or not this collection is empty
if( $collection->isEmpty() ) echo 'empty collection!';
Returns the amount of items as an integer
$amount = $collection->count();