Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Conditionable Laravel Package

illuminate/conditionable

Add fluent, chainable conditional logic to Laravel objects. The Conditionable trait provides when/unless helpers to apply callbacks only when conditions are met, keeping pipelines and builders clean and readable without scattered if/else blocks.

View on GitHub
Deep Wiki
Context7

Getting Started

illuminate/conditionable adds a when() method to any class that uses the Conditionable trait, enabling fluent conditional method chaining. To start:

  1. Require the package: composer require illuminate/conditionable
  2. Use the trait in your class:
    use Illuminate\Support\Conditionable;
    
    class MyService {
        use Conditionable;
    }
    
  3. First use case: conditionally call methods in a chain:
    return (new MyService)
        ->when($condition, fn($s) => $s->methodA())
        ->when(!$condition, fn($s) => $s->methodB());
    

Implementation Patterns

  • Fluent query building / DTO construction: Avoid if/else blocks when building objects:
    $dto = new UserDto()
        ->when($isActive, fn($dto) => $dto-> setActive(true))
        ->when($role === 'admin', fn($dto) => $dto-> grantPermission('write'));
    
  • Conditional middleware or config注入: In Laravel services or custom collectors, decide what to attach at runtime:
    $queueable = (new Job())
        ->when(config('app.debug'), fn($j) => $j->onQueue('debug'));
    
  • Combine with unless() (optional helper): Some projects add unless() as a complement:
    ->unless($skipValidation, fn($s) => $s->validate())
    

Gotchas and Tips

  • Trait method signature: when($condition, callable $callback) returns the original instance if $condition is false—not null. This enables chaining safely.
  • No side effects: The closure receives $this (the instance), so avoid modifying external state directly; prefer returning modified copies or chaining.
  • IDE autocompletion: Since when() is added via trait, some IDEs may not infer return types correctly—annotate the trait usage or use @method in the class docblock:
    /**
     * @method self when(callable $callback)
     */
    
  • Custom traits: If you need when() across many classes, define your own trait that uses Conditionable, then use that trait instead.
  • Laravel-specific tip: Laravel’s Collection and LazyCollection already use Conditionable, so collect([...])->when(...) works out of the box.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4