Collections
Learn how to work with collections of Dtos using DataCollection.
What are Collections?
Section titled “What are Collections?”Collections allow you to work with multiple Dto instances as a group:
$users = UserDto::collection($userArray);// DataCollection of UserDto instances
$users->filter(fn($user) => $user->age > 18);$users->map(fn($user) => $user->name);$users->first();$users->count();Creating Collections
Section titled “Creating Collections”From Array
Section titled “From Array”use Tests\Utils\Docu\Dtos\UserDto;
$data = [ ['name' => 'John', 'age' => 25], ['name' => 'Jane', 'age' => 30],];
$users = UserDto::collection($data);// Result: DataCollection of UserDto instancesFrom Eloquent Collection
Section titled “From Eloquent Collection”$users = User::all();$dtos = UserDto::collection($users);Using DataCollection::make()
Section titled “Using DataCollection::make()”use event4u\DataHelpers\SimpleDto\DtoCollection;use Tests\Utils\Docu\Dtos\UserDto;
$data = [ ['name' => 'John', 'email' => 'john@example.com', 'age' => 30], ['name' => 'Jane', 'email' => 'jane@example.com', 'age' => 25],];$collection = DtoCollection::make($data, UserDto::class);Collection Methods
Section titled “Collection Methods”Filter
Section titled “Filter”use Tests\Utils\Docu\Dtos\UserDto;
$data = [ ['name' => 'John', 'age' => 25], ['name' => 'Jane', 'age' => 17], ['name' => 'Bob', 'age' => 30],];
$users = UserDto::collection($data);$adults = $users->filter(fn($user) => $user->age >= 18);// Result: DataCollection with 2 items (John and Bob)use Tests\Utils\Docu\Dtos\UserDto;
$data = [ ['name' => 'John', 'age' => 25], ['name' => 'Jane', 'age' => 17], ['name' => 'Bob', 'age' => 30],];
$users = UserDto::collection($data);$adults = $users->filter(fn($user) => $user->age > 18);$mapped = $adults->map(fn($user) => UserDto::fromArray([ 'name' => strtoupper($user->name), 'age' => $user->age,]));// Result: DtoCollection with uppercase names for adults (JOHN and BOB)First / Last
Section titled “First / Last”use Tests\Utils\Docu\Dtos\UserDto;
$data = [ ['name' => 'John', 'age' => 25], ['name' => 'Jane', 'age' => 30],];
$users = UserDto::collection($data);$first = $users->first();$last = $users->last();// Result: $first->name = 'John', $last->name = 'Jane'use Tests\Utils\Docu\Dtos\UserDto;
$data = [ ['name' => 'John', 'age' => 25], ['name' => 'Jane', 'age' => 30],];
$users = UserDto::collection($data);$count = $users->count();// Result: 2ToArray
Section titled “ToArray”use Tests\Utils\Docu\Dtos\UserDto;
$data = [ ['name' => 'John', 'age' => 25], ['name' => 'Jane', 'age' => 30],];
$users = UserDto::collection($data);$array = $users->toArray();// Result: [['name' => 'John', 'age' => 25], ['name' => 'Jane', 'age' => 30]]Pagination
Section titled “Pagination”Basic Pagination
Section titled “Basic Pagination”$paginated = UserDto::paginatedCollection($users, page: 1, perPage: 10);// [// 'data' => [...],// 'meta' => [// 'current_page' => 1,// 'per_page' => 10,// 'total' => 100,// 'last_page' => 10,// ],// ]Laravel Pagination
Section titled “Laravel Pagination”$users = User::paginate(10);$dtos = UserDto::collection($users);Nested Collections
Section titled “Nested Collections”class OrderDto extends SimpleDto{ public function __construct( public readonly int $orderId, public readonly DataCollection $items, // Collection of OrderItemDto ) {}}
$order = OrderDto::fromArray([ 'orderId' => 123, 'items' => [ ['product' => 'Widget', 'quantity' => 2], ['product' => 'Gadget', 'quantity' => 1], ],]);Best Practices
Section titled “Best Practices”Use Type Hints
Section titled “Use Type Hints”// ✅ Good - with type hintpublic readonly DataCollection $items;
// ❌ Bad - no type hintpublic readonly $items;Use Collection Methods
Section titled “Use Collection Methods”// ✅ Good - use collection methods$adults = $users->filter(fn($user) => $user->age >= 18);
// ❌ Bad - manual loop$adults = [];foreach ($users as $user) { if ($user->age >= 18) { $adults[] = $user; }}Code Examples
Section titled “Code Examples”The following working examples demonstrate this feature:
- Data Collection - Working with collections
- Dto Sorting - Sorting Dtos in collections
All examples are fully tested and can be run directly.
Related Tests
Section titled “Related Tests”The functionality is thoroughly tested. Key test files:
- CollectionTest.php - Collection tests
Run the tests:
# Run teststask test:unit -- --filter=CollectionSee Also
Section titled “See Also”- Nested Dtos - Complex nested structures
- Creating Dtos - Creation methods
- Type Casting - Automatic type conversion