Conditional Query Logic in Laravel: Mastering when() in the Query Builder
In Laravel’s Query Builder, when() is a fluent helper for conditionally modifying a query. It lets you apply additional clauses only if a condition is met, without breaking your chain with if statements.
What when() Does
// Signature (conceptual)
Builder::when(mixed $value, callable $callback, ?callable $default = null)
* $value: The