https://sebastiandedeyne.com/new-instance-of-singleton-in-laravel/ https://sebastiandedeyne.com/tags/laravel/In Laravel, you can register a class as a https://laravel.com/docs/10.x/container#binding-a-singleton to always resolve the same object. However, you might want to build another instance of the class. You could manually construct the class without Laravel’s container, but if it has a bunch of dependencies it can be tedious. With the build method, Laravel won’t resolve a registered instance of the class, but build a new one with the container. // AppServiceProvider::register() $this->app->singleton(MastodonClient::class); // Resolve the singleton instance from the container $mastodon = resolve(MastodonClient::class); // Build a new instance $anotherMastodon = app()->build(MastodonClient::class); This can be useful when a Laravel package registers a class as a singleton but you need another instance.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners