Laravel Collections: Hidden Gems You Should Know
December 24, 2025
•
1 min read
•
23 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 Query Builder: Optimize Your Database Queries
Learn how to use Laravel's query builder efficiently to optimize your database queries and improve performance.
Livewire Real-Time Validation Made Easy
Implement real-time form validation in Livewire without writing a single line of JavaScript.
Livewire Component Lifecycle Hooks Explained
Master Livewire's lifecycle hooks to control your component behavior at every stage.
Comments (0)
No comments yet. Be the first to comment!