tightenco/collect
A standalone port of Laravel’s Illuminate\Support\Collection for non-Laravel projects. Get expressive, chainable, higher-order methods like map, filter, reduce, pluck, groupBy, and more with minimal dependencies—ideal for any PHP app.
Installation Add the package via Composer:
composer require tightenco/collect
No additional configuration is required—it’s a drop-in replacement for Laravel’s built-in Illuminate\Support\Collection.
First Use Case
Replace Laravel’s Collection with Tightenco\Collect\Collection in your code:
use Tightenco\Collect\Collection;
$items = collect([1, 2, 3]); // Now using Tightenco's Collection
$doubled = $items->map(fn($item) => $item * 2);
Where to Look First
Data Transformation
Use map, pluck, or transform for iterative data manipulation:
$users = collect([
['name' => 'John', 'age' => 30],
['name' => 'Jane', 'age' => 25],
]);
$names = $users->pluck('name'); // ['John', 'Jane']
$ages = $users->map(fn($user) => $user['age'] + 1); // [31, 26]
Filtering and Searching
Leverage filter, where, or reject for conditional logic:
$activeUsers = $users->where('active', true);
$adults = $users->filter(fn($user) => $user['age'] >= 18);
Aggregation
Use sum, avg, count, or groupBy for analytics:
$totalAge = $users->sum('age'); // 55
$groupedByAge = $users->groupBy(fn($user) => $user['age'] > 25 ? 'adult' : 'minor');
Chaining and Fluency Chain methods for concise, readable pipelines:
$result = $users
->where('active', true)
->pluck('name')
->sort()
->values();
Integration with Laravel
Model::all() collections:
use Tightenco\Collect\Collection;
$posts = Post::all(); // Now returns Tightenco\Collect\Collection
Custom Collections
Extend Tightenco\Collect\Collection for domain-specific logic:
class UserCollection extends \Tightenco\Collect\Collection {
public function activeOnly() {
return $this->where('active', true);
}
}
Method Differences
forPage(), macro()) may behave differently or be missing. Verify against Laravel’s Collection docs.macro() is not supported in tightenco/collect—use traits or custom classes instead.Performance
foreach + collect()->where()). Pre-filter data when possible.tap() to debug intermediate steps without breaking the chain:
$users->tap(fn($c) => dump($c))->where('active', true);
Serialization
toArray() explicitly when passing to APIs or storage:
$json = json_encode($collection->toArray());
Compatibility
Collection mixins (e.g., Str::limit()), they won’t work here. Reimplement or use native PHP functions.Collection class. Test thoroughly after swapping.Dump Methods
Use dump() or dd() (from Laravel’s Illuminate\Support\Facades) for inspection:
use Illuminate\Support\Facades\Dump;
Dump::collect($collection); // Pretty-prints the collection
Type Checking Verify the collection class at runtime:
if ($collection instanceof \Tightenco\Collect\Collection) {
// Custom logic for Tightenco's Collection
}
Custom Macros
Add static methods to the Collection class via a trait or helper:
\Tightenco\Collect\Collection::macro('snakeKeys', function () {
return $this->mapWithKeys(fn($item) => [
Str::snake(key($item)) => $item,
]);
});
Global Replacement
Override Laravel’s Collection facade in config/app.php:
'aliases' => [
'Collect' => Tightenco\Collect\Facades\Collect::class,
],
Note: This may conflict with other packages. Use cautiously.
Performance Optimizations
each() instead of map() if you don’t need the results:
$collection->each(fn($item) => $item->process());
tightenco/collect has no settings—it’s a pure drop-in.config(), ensure your app’s environment is loaded before collection operations.Immutable Operations
Prefer map() over direct array manipulation for predictability:
// Bad: Mutates the original
$collection->all['key'] = 'value';
// Good: Returns a new collection
$collection->put('key', 'value');
Lazy Loading
Use lazy() for large datasets to defer execution:
$lazy = $users->lazy()->map(fn($user) => $user->load('posts'));
Testing
Mock collections in PHPUnit with createCollection():
$mock = \Tightenco\Collect\Collection::make([1, 2, 3]);
$this->assertEquals([2, 4, 6], $mock->map(fn($i) => $i * 2));
How can I help you explore Laravel packages today?