skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    Thank you everyone for the help on this. I've eventually found out the originally code had an env file rotator. the content of the env file would hold some text and the system would then use that text with the .env extension to load that specific file. after removing all that all the above suggestions work. I've marked Joe's answer as correct as it is technically correct but not the resolution for this.

    If anyone else comes across a similar issue, for me in the bootstrapapp.php file there was an include to an environment.php file in the same folder. removing this include solved the issue and returned it back to expected outputs.


  2. 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: configapp.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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search