Laravel Collections: Hidden Gems You Should Know
December 24, 2025
•
1 min read
•
77 views
Table of Contents
Laravel collections have many powerful methods:
Partition - Split Based on Condition
[$admins, $users] = User::all()->partition(fn($user) => $user->isAdmin());
Pipe - Transform the Collection
$result = collect([1, 2, 3])->pipe(function ($collection) {
return $collection->sum();
});
When - Conditional Manipulation
$collection = collect([1, 2, 3])
->when(true, fn($c) => $c->push(4));
These methods help you write more expressive and maintainable code.
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!