← BACK
Laravel / Configuration

Conditional Dependency Binding with #[BindWhen] in Laravel 13.22

Punyapal Shah • • 1 min read
edit this tip →

Laravel 13.22 introduces the #[BindWhen] attribute for declarative, conditional service container bindings directly on implementation classes.

Instead of cluttering AppServiceProvider with conditional bind logic, use the #[BindWhen] attribute directly on implementation classes. The closure receives the container instance to resolve runtime conditions.

use Illuminate\Container\Attributes\BindWhen;
use Illuminate\Contracts\Container\Container;

#[BindWhen(
    PaymentGateway::class,
    fn (Container $app) => $app->make('config')->get('services.gateway') === 'stripe'
)]
class StripeGateway implements PaymentGateway {}
  • Closure receives Container instance for runtime decisions
  • Can be repeated: declaration order determines priority
  • Requires PHP 8.5 for closure-in-attribute support

Related Tips

View all tips →
Laravel Configuration

Use config() Instead of env() in Application Code

Call env() only inside configuration files and use config() throughout your application code to prevent missing values when configuration is cached.

READ >
Laravel Configuration

Monthly Log Rotation with Laravel 13.23

Laravel 13.23 adds a built-in monthly logging driver that rotates log files once per month instead of daily.

READ >

// Got a tip in mind? Contributions are always welcome