Accessing Laravel .env variables in blade

VERY IMPORTANT

All env() like: env('APP_ENV') calls WON'T WORK in production (when you use php artisan config:cache)

What to use?

  • use env() only in config files

  • use App::environment() for checking the environment (APP_ENV in .env).

  • use config('app.var') for all other env variables, ex. config('app.debug')

  • create own config files for your own ENV variables. Example:
    In your .env:

    MY_VALUE=foo

example config/myconfig.php

return [
    'myvalue' => env('MY_VALUE', 'bar'), // 'bar' is default if MY_VALUE is missing in .env
];

Access in your code:

config('myconfig.myvalue') // will result in 'foo'

More details see HERE


Five most important commands if your Laravel is not working as expected after some modifications in .env or database folder or because of any other modifications. Here is full explanation: https://www.youtube.com/watch?v=Q1ynDMC8UGg

php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan view:clear
php artisan route:clear

I have it implemented in the following way:

@if (env('APP_ENV')!='Production')
Enviroment Test
@endif

My recommendation is to execute the following command: composer self-update

Tags:

Php

Laravel