Laravel

Laravel Collections: Hidden Gems You Should Know

December 24, 2025 1 min read 23 views

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.

Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!