Interview frequency: …
Short answer
bind creates a new instance on each resolution, singleton keeps one instance for the application's lifetime, and scoped keeps one instance per HTTP request and clears it before the next request.
Explanation
The Service Container manages dependency resolution in Laravel. Use bind for lightweight or stateless services and singleton for services with expensive connections or shared state. scoped was added specifically for Long-Running environments (such as Laravel Octane), where all operations in one request should share an instance, but it must be destroyed when the request ends to avoid leaking user data.
Code example
// A new instance for each request
$this->app->bind(Transactor::class, StripeTransactor::class);
// One instance forever
$this->app->singleton(DatabaseConnection::class, fn() => new DatabaseConnection());
// One instance per request HTTP
$this->app->scoped(CurrentTenant::class, fn() => new CurrentTenant());Common mistakes
- Using singleton for objects that store data about a specific user in Laravel Octane environments.
Follow-up questions
- How does Laravel Octane improve application performance, and what are the risks of in-memory State Leaks?
- How do register() and boot() differ in Laravel Service Providers, and when should each be used?