136

I am trying to get some API keys which I have stored in my .env file to use in the blade javascript. I have added two keys like:

APP_ENV=local
APP_KEY=////
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
APP_GOOGLE_MAPS=////
APP_OVERHEID_IO=////

In blade I need to use the Google Maps API and OverheidIO API key. I have tried getting one of the default .env variables just in case I have formatted the custom .env variables wrong.:

{{ env('APP.ENV') }} // nothing
{{ env('APP_ENV') }} // nothing
{{ env('APP_ENV'), 'test' }} // returns 'test' 

Could someone help me call the google maps api and overheidio api key in the blade?

Anna Jeanine
  • 3,975
  • 10
  • 40
  • 74
  • 20
    Try to run `php artisan config:clear` and test `env('APP_ENV')` in tinker. For me it returns `local` string. – Alexey Mezenin Mar 27 '17 at 08:07
  • Thank you Alexey! `php artisan config:clear` did the trick. – Anna Jeanine Mar 27 '17 at 08:09
  • @Alexey Mezenin, is it not fine if you post your comment as Answer?. I am pretty new to using SO & not very clear about the rules. – manian Mar 27 '17 at 08:12
  • @YevgeniyAfanasyev that question is a duplicate of mine, I asked it before... – Anna Jeanine Nov 20 '18 at 11:09
  • @AnnaJeanine, [here](https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled) you'll find a discussion about duplicate questions. – Yevgeniy Afanasyev Nov 20 '18 at 23:27
  • @AnnaJeanine, don't take it personally, it is a good question. I voted it up, and gave it an answer. But it is a duplicate. – Yevgeniy Afanasyev Nov 20 '18 at 23:50
  • @YevgeniyAfanasyev the question you flagged as it being a duplicate of was asked after I asked my question, how can this then be a duplicate of that if I asked the question first. – Anna Jeanine Nov 21 '18 at 09:08
  • @AnnaJeanine, sorry, I may be wrong, I thought the other question is better stated. But it is not up to me to decide, it is up to community to make a decision, I hope, that people with admin privileges would see situation clearer than me and make a right choice of that is an original and what is a duplicate. – Yevgeniy Afanasyev Nov 21 '18 at 22:06
  • Attention, env('APP_ENV') will fail in production, see https://stackoverflow.com/a/57626957/4820095 – ndberg Aug 23 '19 at 13:07

16 Answers16

170

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.

php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan view:clear
php artisan route:clear
miken32
  • 42,008
  • 16
  • 111
  • 154
Abhay Maurya
  • 11,819
  • 8
  • 46
  • 64
  • 1
    Exactly the reason sometimes, Laravel 5.5 has a hard caching system – Deepesh Thapa Nov 17 '18 at 10:58
  • 4
    good stuff, but attention, env('APP_ENV') will fail in production, see https://stackoverflow.com/a/57626957/4820095 – ndberg Aug 23 '19 at 13:07
  • 1
    @ndberg that is when you use the command `php artisan config:cache` and I am not using that one. Please read. – Abhay Maurya Aug 26 '19 at 10:46
  • Hi @ndberg I double reviewed your answer to this question (link in the comment above) and you refer to `php artisan config:cache` as @Learner mentions. BUT, I am not sure if Learner's answer needs to be edited to add the missing command, or if you made a mistake. Please, clarify it. – Guillermo Garcia Jun 08 '20 at 10:12
  • 1
    @Learner is right, he does not use it in his answer and it's not needed. But I just wanted to outline for all googler: usually in production, we use config:cache, and eventually the call as used in the question: "env('APP.ENV')" will bring troubles. So it just was an addition to this answer. – ndberg Jun 08 '20 at 14:45
  • 3
    This shouldn't be the accepted answer, there is a reason `env` variables don't work when you have a cache enabled and they'll stop working as soon as you reenable the cache, see below – Tofandel Feb 16 '23 at 21:42
60

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

ndberg
  • 3,391
  • 1
  • 21
  • 36
  • 4
    What @ndberg says here is key: `use env() only in config files`. This is the best answer to this question. – Pathros Oct 31 '20 at 04:04
  • I like this answer but I believe there is an small issue. The config files must be placed into the config folder. In this example "config/myconfig.php" must be used instead of "app/myconfig.php" – Luis Rodriguez Aug 31 '22 at 14:54
  • You're right, I updated it. – ndberg Sep 01 '22 at 15:13
  • This answer seems to imply you can't use the `config()` helper to check the environment - why and what's the source for this info? – Hashim Aziz Jul 25 '23 at 23:44
  • Nope, it's no problem to use the config() helper. But you should not use the env() helper outside of config files. – ndberg Jul 27 '23 at 21:12
43

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

ifconfig
  • 6,242
  • 7
  • 41
  • 65
Armando Cordova
  • 739
  • 7
  • 5
22

You should only access .env values directly inside configuration files, then access them from everywhere (controllers, views) from configuration files using config() helper

For example:

.env

TEST_URL=http://test

config/app.php

return [
   'test_url' => env('TEST_URL','http://default.url')
];

resources/views/welcome.blade.php

{{ config('app.test_url')}}

see configuration caching from laravel documentation for more info.

medard mandane
  • 549
  • 4
  • 6
13

Since Laravel 7.11, you can use the @env('') and @production() directives in blade templates:

@env('staging')
    // The application is running in "staging"...
@endenv

@env(['staging', 'production'])
    // The application is running in "staging" or "production"...
@endenv

or

@production
    // Production specific content...
@endproduction

See also in the Laravel Blade Documentation.

Attila Fulop
  • 6,861
  • 2
  • 44
  • 50
12

If you want to get the environment of the app then try this:

{{App::environment()}}

I have not tried other variables.

Jnanaranjan
  • 1,304
  • 15
  • 31
5
php artisan config:clear

should fix it

Luca C.
  • 11,714
  • 1
  • 86
  • 77
5

It causes problems to use env() anywhere else than in the config/ folder. Use env in there and then config () in the other parts of the app

Alex
  • 32,506
  • 16
  • 106
  • 171
5

get values here: config/app.php


in blade:

{{ config('app.name', 'default value here') }}

in class/controller:

config('app.name', 'default value here')
Mhar Daniel
  • 475
  • 5
  • 11
5

Here's a link to the documentation: https://laravel.com/docs/6.x/configuration#retrieving-environment-configuration

In the sample below, I spit out the actual error when I'm in my development environment but give a generic message if in any other environment.

@if(App::environment('development'))
    Error: {{ $record->s_error }}
@else
    XML Parsing Error - Please double check that your file is formatted correctly.
@endif
JR Lawhorne
  • 3,192
  • 4
  • 31
  • 41
5
{{ env('APP_ENV') }}

**If It doesn't work then run the below command. It worked for me. You can try. This command will clear the configuration cache. **

php artisan config:clear
Pushpak
  • 328
  • 3
  • 10
3

Never use env() anywhere in your code, other than inside config/*.php

Use config() to access default/custom variables in blade files/Controllers.

For example

config('app.name')

config('app.env')

where app is the file name inside the config directory. you can use any file name inside config directory to access the variable inside it.

Although answers by others are right, i found it hard to understand, so finally read the whole documentation for config page.

Important comment from documentation.

Once the configuration has been cached, the .env file will not be loaded; therefore, the env function will only return external, system level environment variables.

Gopal Sharma
  • 755
  • 1
  • 12
  • 25
0

This command should be written after you edit .env file to access variables in easy way

php artisan config:cache
Ahmed Mahmoud
  • 1,724
  • 1
  • 17
  • 21
0

i was also having trouble getting value from .env file, then i did this and it helped :

  1. Check env file and see if you have given the correct value.
  2. then check blade or controller where you using that variable from .env file.
  3. if both steps above goes right, you just need to do these steps -

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

0

Run the following command.

php artisan optimize
Smart Boy
  • 147
  • 1
  • 8
0
if (env('APP_ENV')=='Production')
 do something login,verify etc
else
  do something
  • Here env('APP_ENV') won't work in production so better to get from config folder and make your own file and access that.

  • Ex:-config/customconfig.php ->make new file

    env('APP_ENV'), ];

and then you can access like this

if (config('customconfig.appenv')=='Production')
     do something login,verify etc
    else
      do something 
  • and final run php artisan config:cache