Collections

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:

use Cuisine\Wrappers\Collection


Creating collections

Using the Collection facade, you can easily create collection from any array, like so:

$collection = Collection::make([ 1, 2, 3 ]);

Return values

A collection uses a return value to determine what to return. In a default collection this takes just two forms:

toArray()

$items = $collection->toArray()->all();

This example turns the collection into an array and then gives all items back with the all() method.

toJson()

$json = $collection->toJson()->all();

This example sets the returnValue to json and then returns all items as a (you guessed it) json string.


Available methods

All()

Returns all items, in the set return value (defaults to array)

$allItems = $collection->all();

Get( $key, $default = null )

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' );

First()

Returns the first value of a collection:

$firstValue = $collection->first();

isEmpty()

Returns a boolean on wether or not this collection is empty

if( $collection->isEmpty() ) echo 'empty collection!';

Count()

Returns the amount of items as an integer

$amount = $collection->count();