Use when(), unless(), and whenEmpty() on Laravel Collections to conditionally apply transformations without breaking method chains.
When processing collections (such as applying search filters or sorting parameters), writing intermediate if blocks breaks the fluent chain and requires temporary variables.
Laravel Collections provide when() and unless() to conditionally execute closures.
Conditional Transformations with when()
$sortBy = request('sort'); // 'price', 'rating', or null
$filterActive = request()->boolean('active_only');
$products = collect($rawProducts)
->when($filterActive, function ($collection) {
return $collection->where('is_active', true);
})
->when($sortBy === 'price', function ($collection) {
return $collection->sortBy('price');
})
->when($sortBy === 'rating', function ($collection) {
return $collection->sortByDesc('rating');
});
Fallback Data with whenEmpty()
When a collection contains no items, whenEmpty() provides a clean fallback closure:
$recipients = collect($users)
->whenEmpty(function ($collection) {
// Fallback to default administrator if no recipients found
return collect([User::getSystemAdmin()]);
});
Summary
- Keeps collection transformation logic inside a single readable fluent chain.
- Accepts an optional default callback as the third argument if the condition is false.
whenEmpty()cleanly handles empty dataset fallbacks.
Tags:
Laravel Collections Clean Code