Using Collections¶
This section explains how to create, manipulate, and perform operations on collections.
Creating a Collection¶
To create a collection, use the ::create() method:
$collection = MyEntity::create();
Common Operations on Collections¶
Exporting Data¶
Collections can be exported as arrays or maps for further processing:
$array = $collection->get($to_array = true);
$map = $collection->get();
Accessing Elements¶
Retrieve the first element in a collection:
$first = $collection->first();
Bulk Updates¶
Apply updates to all objects in the collection:
$collection->update(['status' => 'archived']);
Deleting Objects¶
Delete all objects in a collection:
$collection->delete();
Advanced Operations¶
Filtering¶
Filter collections using domains:
$filtered = MyEntity::search(['status' => 'active']);
Aggregating Data¶
Perform aggregate operations like counting or summing fields:
$count = $collection->count();
// Sum the 'amount' field for all objects in the collection
$sum = 0;
$collection->each(function($id, $object) use (&$sum) {
$sum += $object['amount'];
});
Chaining Methods¶
Combine multiple operations for concise and readable code:
$results = MyEntity::search(['status' => 'active'])
->orderBy('name', 'asc')
->read(['id', 'name']);
For more on domains and filtering, see Domain Filtering.