Dotenv must be an instance of Dotenv\Loader Dotenv must be an instance of Dotenv\Loader codeigniter codeigniter

Dotenv must be an instance of Dotenv\Loader


Ok so changing that hook to this seems to be working, I am not entirely sure it's the correct approach but digging into the library code seems ok.

$hook['pre_system'] = function() {    $dotenv = Dotenv\Dotenv::create(__DIR__);    $dotenv->load();}

If this is wrong for any reason please let me know. Thanks


I tried all solutions then I found that my version of phpdotenv was 4.x.x. For those who have a confusion of why the solutions above doesn't work.

Here is the new way to load the env with path as the constructor param:

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__.'/..');$dotenv->load();


Dotenv must be an instance of Dotenv\Loader

Actually you are not following documentation of upgrading Laravel from any version to 5.8, I have found solution after searching of few hours. Finally i found the solution. You just need to replace this code in your environment file.

$env = $app->detectEnvironment(function(){$environmentPath = __DIR__.'/../.env';$setEnv = trim(file_get_contents($environmentPath));if (file_exists($environmentPath)){    putenv('APP_ENV='.$setEnv);    if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {        $dotenv = Dotenv\Dotenv::create(__DIR__.'/../', '.'.getenv('APP_ENV').'.env');        $dotenv->overload();    }}});

Here is a link where you can check in details how to use multiple env files in laravel 5.8. Reference Link

Enjoy the coding . . . !!!