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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners