pragmarx/coollection
Coollection repackages Laravel-style collections (via Tightenco\Collect) to let you access items as object properties. Traverse nested arrays/objects with fluent collection methods, then read values like $countries->where('name.common','US')->first()->currency->name.
Installation:
composer require pragmarx/coollection
This adds the coollect() helper and Coollection class to your project.
First Use Case:
Convert a standard array or Laravel Collection into a Coollection for object-like property access:
$users = coollect([
['name' => 'John', 'age' => 30],
['name' => 'Jane', 'age' => 25]
]);
// Access properties directly
echo $users->first()->name; // "John"
Key Features to Explore:
$collection->nested->key).$collection->rio or $collection->RIO for keys like rio.where, filter, map).API Response Handling:
Transform API responses (e.g., JSON) into Coollection for intuitive access:
$response = coollect($apiResponse['data']);
$user = $response->first()->profile->name; // Direct property chaining
Request Data Processing:
Convert Laravel request data into a Coollection for cleaner validation or manipulation:
$input = coollect(request()->all());
$pageSize = $input->pagination->perPage; // Instead of $input->get('pagination.perPage')
Nested Data Filtering: Chain methods for complex queries:
$filtered = $products
->where('category', 'electronics')
->sortBy('price')
->pluck('name')
->implode(', ');
Dynamic Property Access:
Use ia-str (dependency) for flexible key handling (snake_case, camelCase, etc.):
$data->user_first_name; // Accesses 'user_first_name' or 'userFirstName'
collect(): Use coollect() instead of collect() for projects where object-like access is preferred.Coollection in method signatures for clarity:
public function process(Coollection $data): Coollection { ... }
Coollection as a bindable interface if extending functionality:
$this->app->bind(CollectionInterface::class, Coollection::class);
Key Sensitivity:
$collection->rio or $collection->RIO).user_name) require exact casing unless using ia-str utilities.Performance:
$collection['key']). Benchmark critical paths.->first()->nested->method()).Laravel Version Compatibility:
illuminate/collections).illuminate/collections:^10.48|^11.29).Dot Notation Limitations:
null) will throw errors:
$collection->non_existent->key; // Throws: "Property [non_existent] does not exist."
has() or contains() to verify keys before access:
if ($collection->has('key')) {
$value = $collection->key;
}
$collection->toArray() to debug or fall back to standard methods:
$value = $collection->get('key', $collection->toArray()['key'] ?? null);
Coollection to add methods for domain-specific logic:
class UserCollection extends Coollection {
public function activeUsers() {
return $this->where('status', 'active');
}
}
ia-str to customize key normalization (e.g., force snake_case):
$collection = coollect([], ['key_case' => 'snake']);
use IlluminateExtracted\Support\Collection;
$collection = new Collection(['data']);
config/coollection.php (if provided by the package) or environment variables:
'default_key_case' => 'snake', // Forces snake_case for all collections
coollect() conflicts, alias it in composer.json:
"extra": {
"aliases": {
"coollect": "pragmarx/coollection:coollect"
}
}
```markdown
### Pro Tips
- **Combine with Laravel**:
Use `Coollection` in Eloquent models for cleaner relationships:
```php
$user->posts->published()->count(); // Instead of $user->posts()->where('published', true)->count()
Coollection in tests with createMock(Coollection::class) for predictable behavior.collect() calls with coollect() using IDE refactoring tools.How can I help you explore Laravel packages today?