phplaravelphpdotenv

Laravel 10 - Getting the env returns the entire .env file


I have tried a few different ways to do this, but if I do env("APP_ENV") I get the entires contents of the env file instead of the APP_ENV value

I know this is bad practice but I also get the same results when using Config

I have tried artisan cache:clear and artisan config:clear and still the same results.

// dd(Config::get('env')); // this does the same 
dd(Config::get('app.APP_ENV'), Config::get('app.EMAILMODE'), env("APP_ENV"));

returns

"""
APP_NAME=Laravel


APP_ENV="local"


APP_KEY=*removed*


APP_DEBUG=true


APP_URL=*removed*



LOG_CHANNEL=stack


LOG_DEPRECATIONS_CHANNEL=null


LOG_LEVEL=debug



DB_CONNECTION=sqlsrv


DB_HOST=*removed*


DB_PORT=1433


DB_DATABASE=*removed*


DB_USERNAME=*removed*


DB_PASSWORD=*removed*


DB_ENCRYPT=true


DB_TRUST_SERVER_CERTIFICATE=true

""" // app/Http/Controllers/SecurityScanController.php:32

"testing" // app/Http/Controllers/SecurityScanController.php:32

"""
APP_NAME=Laravel


APP_ENV="local"


APP_KEY=*removed*


APP_DEBUG=true


APP_URL=*removed*



LOG_CHANNEL=stack


LOG_DEPRECATIONS_CHANNEL=null


LOG_LEVEL=debug



DB_CONNECTION=sqlsrv


DB_HOST=*removed*


DB_PORT=1433


DB_DATABASE=*removed*


DB_USERNAME=*removed*


DB_PASSWORD=*removed*


DB_ENCRYPT=true


DB_TRUST_SERVER_CERTIFICATE=true

""" // app/Http/Controllers/SecurityScanController.php:32


Solution

  • The correct syntax to obtain from config file is the following:

    config('app.env')
    

    instead of Config::get('app.APP_ENV') , where app refers to the file itself: config\app.php and the next string env refers to the key array on that file.

    If you want to read directly from the env variables, the syntax is:

    env('APP_ENV')
    

    I would check for unclosed quotes in the .env file that may be invalidating it. Also, when the value is a single word, you don't need to enclose it in quotes.

    APP_ENV=local
    

    works just fine.