
Conditionable trait: when() & unless()
The Conditionable trait, injects two methods into classes (e.g. Query Builder , Request , Fluent, or your own classes):
- when($value, $callback, $default = null): Executes $callback if $value is truthy.
- unless($value, $callback, $default = null): Executes $callback if $value is falsy
Why it’s useful
- Maintains structural clarity: helps build condition-based variations (e.g., queries, request prep, mail notifications) inline
- Highly reusable: not limited to built-in Laravel classes you can include it in any custom class .
- Used across Laravel (Query Builder, Request, Fluent, Collections, etc.) and available for custom usage.
Back