Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zer0uno's avatar

Load different service providers based on the environment

In Laravel 12 how can I load different service providers based on the environment? I think the documentation is lacking on this topic.

So far I've found 3 potential places:

  • inside the bootstrap/providers.php using app()->environment() BUT in case of "php artisan make:provider" it will be overwritten
  • inside the config/app.php under the "providers" key BUT I'm not sure if this property is still supported because I can find to reference in the docs, I've just found it looking at the source code
  • inside the specific service provider calling $this->app->environment() BUT it requires the logic to be replicated in each service provider
1 like
1 reply
vincent15000's avatar

You can do that from inside the AppServiceProvider class.

public function register()
{
    // Load providers only in local environment
    if ($this->app->environment('local')) {
        $this->app->register(\App\Providers\LocalOnlyServiceProvider::class);
    }

    // Load providers only in production
    if ($this->app->environment('production')) {
        $this->app->register(\App\Providers\ProductionServiceProvider::class);
    }
}

Please or to participate in this conversation.