Laravel 12 Service Container: Contextual Binding Patterns I Reach For Every Time
Contextual binding in Laravel's service container is one of those features that looks like a footnote until you're wiring up a multi-tenant app or juggling multiple implementations of the same interface. Here's how I actually use it.
Why basic interface binding isn't always enough
The standard pattern — $this->app->bind(LoggerInterface::class, SlackLogger::class) — works until you have two classes that both depend on LoggerInterface but need different implementations. Maybe your payment processor should log to a dedicated channel and everything else goes to the default stack. With a single binding you're stuck. Contextual binding is the escape hatch.
The basic syntax in Laravel 12
Laravel's container has supported contextual binding since at least version 5.x, but in Laravel 12 (with the leaner application skeleton introduced in 11) it fits even more naturally because there's less boot scaffolding obscuring what's happening in your service providers.
// AppServiceProvider::register()
$this->app
->when(PaymentService::class)
->needs(LoggerInterface::class)
->give(fn () => new ChannelLogger('payments'));
$this->app
->when(OrderService::class)
->needs(LoggerInterface::class)
->give(fn () => new ChannelLogger('orders'));The closure form of give() is important when your alternative implementation itself has dependencies — it defers resolution until the class is actually instantiated rather than eagerly building the object at boot time.
Giving a tagged group instead of a single implementation
One pattern I reach for in plugin-style architectures is tagging several implementations and injecting the whole collection into one consumer while giving a specific singleton to another.
// Register several notifiers
$this->app->tag([
EmailNotifier::class,
SmsNotifier::class,
PushNotifier::class,
], 'notifiers');
// OrderCompleted broadcasts to all of them
$this->app
->when(OrderCompletedListener::class)
->needs('$notifiers')
->giveTagged('notifiers');
// PasswordReset only uses email
$this->app
->when(PasswordResetListener::class)
->needs(NotifierInterface::class)
->give(EmailNotifier::class);Notice giveTagged() — it was formalised in Laravel 10 and pairs cleanly with the variable-name binding syntax (needs('$notifiers')) to inject an iterable of resolved instances.
Contextual binding with primitives
You're not limited to interfaces. Primitive config values are where this gets really practical in multi-tenant or multi-environment setups.
$this->app
->when(S3StorageAdapter::class)
->needs('$bucket')
->give(config('filesystems.disks.s3.bucket'));
$this->app
->when(LegacyS3StorageAdapter::class)
->needs('$bucket')
->give(config('filesystems.disks.s3_legacy.bucket'));This keeps your adapter constructors clean — just a typed string $bucket parameter — and keeps the config plumbing out of the class itself. No config() calls buried inside business logic.
Scoping to multiple consumers at once
Since Laravel 11, when() accepts an array, which saves a lot of repetition when several classes share the same non-default implementation.
$this->app
->when([
ReportExporter::class,
InvoiceGenerator::class,
StatementBuilder::class,
])
->needs(StorageInterface::class)
->give(GcsStorageAdapter::class);Where contextual binding breaks down
It's not a silver bullet. A few situations where I've backed away from it:
- Runtime conditionals: If the implementation needs to change based on a value only known at request time (e.g., the authenticated tenant's config row), the container is the wrong layer. Reach for a factory or a strategy pattern resolved from a repository instead.
- Too many consumers: Once you're writing more than five or six
when()->needs()->give()chains for the same interface, that's usually a signal the interface is doing too much and should be split. - Testing clarity: Contextual bindings are invisible in a test unless you boot the full application container. If you're unit-testing a class in isolation, just pass the dependency directly in the constructor — don't rely on container resolution to make the test green.
Practical takeaway
I register all contextual bindings in the register() method of a dedicated provider — something like BindingServiceProvider — rather than spreading them across feature providers. It makes the whole binding map easy to audit in one file. Combined with Laravel 12's minimal default scaffolding, that one file tells you exactly which classes get non-default wiring, which is worth a lot when onboarding someone to a codebase six months after you wrote it.