Collections | Flextype

Collections

Core Concepts

Flextype provide a powerful functionality to work with data collections. You may use it for collecting specific data by specific criterias. For e.g. collect specific blog posts or movies or series and etc..

Operators

Operator Description
eq = Equal to

Filter your collection by checking if your custom attribute (field) has a value that is equal to one of the values provided.

Use-cases:
Get collection that is refered to another in a 1:N relationship or if you want to get collection with a specific value in one of it's fields.
neq <> != Not equal to

Filter your collection by checking if your custom attribute (field) does not have a value that is equal to one of the values provided.
lt < Lower than

Filter your collection by checking if your custom attribute (field) has a value that is lower than one of the values provided.
gt > Greater than

Filter your collection by checking if your custom attribute (field) has a value that is greater than one of the values provided.
lte <= Lower than or equal to

Filter your collection by checking if your custom attribute (field) has a value that is lower than or equal to one of the values provided.
gt >= Greater than or equal to

Filter your collection by checking if your custom attribute (field) has a value that is greater than or equal to one of the values provided.
in Included in an array of values

Filter your collection by checking if your custom array attribute (field) contains one of the values provided. As soon as one of the provided values separated with, are in the array field, the entry object will be in the response.

Use-cases:
Get all content entries that is refered to others in a N:N relationship or if you want to get all entries with a specific value in one of it's array fields.
nin Isn't included in an array of values

Filter your entries by checking if your custom array attribute (field) is not contains one of the values provided.

Use-cases:
Get all content collection that is not refered to others in a N:N relationship or if you want to get all entries with a specific value that is not in one of it's array fields.
contains like Contains the substring

Filter your entries by checking if your custom attribute (field) has a value that is "like" the value provided.
member_of Member of

starts_with Starts with

ends_with Ends with

Methods

Name Description
collect() Returns a new Collection instance with the items currently in the collection.
merge() Merges the given array or collection with the original collection. If a string key in the given items matches a string key in the original collection, the given items's value will overwrite the value in the original collection.
where() Sets the where expression to evaluate when this Criteria is searched for.
andWhere() Appends the where expression to evaluate when this Criteria is searched for using an AND with previous expression.
orWhere() Appends the where expression to evaluate when this Criteria is searched for using an OR with previous expression.
only() Get a subset of the items from the given collection.
orderBy() Sets the ordering of the result of this Criteria. Keys are field and values are the order, being either ASC or DESC.
setFirstResult() Set the number of first result that this Criteria should return.
limit() Sets the max results that this Criteria should return.
first() Returns the current first result option of this Criteria.
last() Returns the current last result option of this Criteria.
next() Returns a single item of result. Moves the internal iterator position to the next element and returns this element.
random() Returns one or a specified number of items randomly from the collection.
shuffle() Returns randomly shuffles the items in the collection.
slice() Returns a slice of the collection by defining length of elements and starting position offset.

If length is null it returns all elements from offset to the end of the Collection. Keys have to be preserved by this method. Calling this method will only return the selected slice and NOT change the elements contained in the collection slice is called on.
count() If you want to know how many items match your Criteria, you can call count().
exists() If you just need to check if any item exist in the collection that match the Criteria, you can call exists(), which will return either true or false.
all() Returns the underlying array represented by the collection.
collect_filter() Apply filters to the array items with help of parameters array.

Methods Details

collect()

Returns a new Collection instance with the items currently in the collection.

Examples

Collect movies

// Fetch movies collections
$movies_collection = flextype('entries')->fetchCollection('movies');

// Create movies collection
$movies = collect($movies_collection);
merge()

Merges the given array or collection with the original collection. If a string key in the given items matches a string key in the original collection, the given items's value will overwrite the value in the original collection

Examples

Collect all movies in genres in one movies collection

// Fetch movies: comedy, drama and fantasy
$comedy  = flextype('entries')->fetchCollection('movies/comedy');
$drama   = flextype('entries')->fetchCollection('movies/drama');
$fantasy = flextype('entries')->fetchCollection('movies/fantasy');

// Create movies collection with comedy, drama and fantasy
$movies = collect($comedy)->merge($drama)->merge($fantasy);
where()

Sets the where expression to evaluate when this Criteria is searched for.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection where field directed_by eq Denis Villeneuve
$movies = collect($drama)->where('directed_by', 'eq', 'Denis Villeneuve');

Collect drama movies where field stars contains Amy Adams

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection where field stars contains Amy Adams
$movies = collect($drama)->where('stars', 'contains', 'Amy Adams');
andWhere()

Appends the where expression to evaluate when this Criteria is searched for using an AND with previous expression.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve and where field year eq 2020

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and where field year eq 2020
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')
            ->andWhere('year', 'eq', 2020);

Collect drama movies where field directed_by eq Denis Villeneuve and where field year eq 2020 and where field stars contains Amy Adams

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and where field year eq 2020
// and where field stars contains Amy Adams
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')
            ->andWhere('year', 'eq', 2020);
            ->andWhere('stars', 'contains', 'Amy Adams');
orWhere()

Appends the where expression to evaluate when this Criteria is searched for using an OR with previous expression.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve and where field year eq 2020 or where year eq 2019

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and where field year eq 2020
// or where field year eq 2019
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')
            ->andWhere('year', 'eq', 2020);
            ->orWhere('year', 'eq', 2019);
only()

Get a subset of the items from the given collection.

Examples

Collect drama movies items: title, id

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Collect drama movies items: title, id
$movies = collect($drama)->only(['title', 'id']);
orderBy()

Sets the ordering of the result of this Criteria. Keys are field and values are the order, being either ASC or DESC.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve and order by year ASC.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and order by year ASC
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')
            ->orderBy('year', 'ASC');

Collect drama movies where field directed_by eq Denis Villeneuve and order by year DESC.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and order by year DESC
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')
            ->orderBy('year', 'DESC');
setFirstResult()

Set the number of first result that this Criteria should return.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve and set first result 5.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and set first result 5
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')
            ->setFirstResult(5);
limit()

Sets the max results that this Criteria should return.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve and set limit 10.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and set limit 10
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')
            ->limit(10);
first()

Returns the current first result option of this Criteria.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve and return first option of this Criteria.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')

$result = $movies->first();
last()

Returns the current last result option of this Criteria.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve and return last option of this Criteria.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')

$result = $movies->last();
next()

Returns a single item of result. Moves the internal iterator position to the next element and returns this element.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve and moves the internal iterator position to the next element and returns this element.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')

$result = $movies->next();
random()

Returns one or a specified number of items randomly from the collection.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve and return 3 random items.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')

$result = $movies->random(3);
shuffle()

Returns randomly shuffles the items in the collection.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve, shuffle the items and return them all.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')

$result = $movies->shuffle();
slice()

Returns a slice of the collection by defining length of elements and starting position offset.

If length is null it returns all elements from offset to the end of the Collection. Keys have to be preserved by this method. Calling this method will only return the selected slice and NOT change the elements contained in the collection slice is called on.

Examples

Collect drama movies where field directed_by eq Denis Villeneuve, and set offset 0 and length 5

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')

$result = $movies->slice(0, 5);
count()

If you want to know how many items match your Criteria, you can call count().

Examples

Count all drama movies where field directed_by eq Denis Villeneuve.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')

$result = $movies->count();
exists()

If you just need to check if any item exist in the collection that match the Criteria, you can call exists(), which will return either true or false.

Examples

Check if any drama movies where field directed_by eq Denis Villeneuve are exists.

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')

$result = $movies->exists();

if ($result) {
    // do something...
}
all()

Returns the underlying array represented by the collection.

Examples

Collect all drama movies where field directed_by eq Denis Villeneuve

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
$movies = collect($drama)
            ->where('directed_by', 'eq', 'Denis Villeneuve')

$result = $movies->all();
collect_filter()

Apply filters to the array items with help of parameters array.

Examples

// Fetch movies drama collection
$drama = flextype('entries')->fetchCollection('movies/drama');

// Create movies drama collection where field directed_by eq Denis Villeneuve
$movies = collect_filter($drama, [
                                    'where' => [
                                        'key' => 'directed_by',
                                        'expr' => 'eq',
                                        'value' => 'Denis Villeneuve'
                                  ]
                              ]);

// Create movies drama collection where field stars contains Amy Adams
$movies = collect_filter($drama, [
                                    'where' => [
                                        'key' => 'directed_by',
                                        'expr' => 'contains',
                                        'value' => 'Amy Adams'
                                    ]
                                ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and where field year eq 2020
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'and_where' => [
                                            [
                                                'key' => 'year',
                                                'expr' => 'eq',
                                                'value' => 2020
                                            ]
                                        ]
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and where field year eq 2020
// and where field stars contains Amy Adams
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'and_where' => [
                                            [
                                                'key' => 'year',
                                                'expr' => 'eq',
                                                'value' => 2020
                                            ]
                                        ],
                                        'and_where' => [
                                            [
                                                'key' => 'stars',
                                                'expr' => 'contains',
                                                'value' => 'Amy Adams'
                                            ]
                                        ]
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and where field year eq 2020
// or where field year eq 2019
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'and_where' => [
                                            [
                                                'key' => 'year',
                                                'expr' => 'eq',
                                                'value' => 2020
                                            ]
                                        ],
                                        'or_where' => [
                                            [
                                                'key' => 'year',
                                                'expr' => 'eq',
                                                'value' => 2019
                                            ]
                                        ]
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and order by year ASC
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'order_by' => [
                                            'field' => 'year',
                                            'direction' => 'ASC'
                                        ]
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and set first result 5
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'set_first_result_value' => 5,
                                        'return' => 'set_first_result'
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and set limit 10
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'limit_value' => 10,
                                        'return' => 'limit'
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and return first option of this Criteria.
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'return' => 'first'
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and return last option of this Criteria.
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'return' => 'last'
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and moves the internal iterator position to the next element and returns this element.
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'return' => 'next'
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and return 3 random items.
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'return' => 'random',
                                        'random_value' => 3
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// shuffle the items and return them all.
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'return' => 'shuffle'
                                    ]);

// Create movies drama collection
// where field directed_by eq Denis Villeneuve
// and set offset 0 and length 5
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'return' => 'slice',
                                        'slice_offset_value' => 0,
                                        'slice_offset_value' => 5,
                                    ]);

// Count all drama movies where field directed_by eq Denis Villeneuve.
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'return' => 'count'
                                    ]);

// Check if any drama movies where field directed_by eq Denis Villeneuve are exists.
$movies = collect_filter($drama, [
                                        'where' => [
                                            'key' => 'directed_by',
                                            'expr' => 'eq',
                                            'value' => 'Denis Villeneuve'
                                        ],
                                        'return' => 'exists'
                                    ]);

On this page

Language: en, ru