121

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,634
  • 10
  • 36
  • 68
  • 17
    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
  • Possible duplicate of [Laravel 5.3 - env() always returns null](https://stackoverflow.com/questions/43243732/laravel-5-3-env-always-returns-null) – Yevgeniy Afanasyev Nov 20 '18 at 03:11
  • @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

159

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
Abhay Maurya
  • 10,668
  • 6
  • 42
  • 58
  • 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
  • @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
41

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 app/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
  • 2,463
  • 18
  • 34
  • 1
    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
39

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
  • 5,502
  • 7
  • 38
  • 63
Armando Cordova
  • 683
  • 7
  • 5
14

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
  • 376
  • 3
  • 5
12

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

{{App::environment()}}

I have not tried other variables.

Jnanaranjan
  • 1,294
  • 15
  • 31
10

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,501
  • 2
  • 43
  • 49
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
  • 29,618
  • 13
  • 100
  • 157
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,062
  • 4
  • 30
  • 39
4

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
  • 395
  • 2
  • 11
3
php artisan config:clear

should fix it

Luca C.
  • 9,980
  • 1
  • 77
  • 73
1

Run the following command.

php artisan optimize
wmc89
  • 61
  • 3
1
{{ 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
  • 87
  • 7
0

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

php artisan config:cache
Ahmed Mahmoud
  • 1,576
  • 1
  • 16
  • 19
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
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
0

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.

Gopal Sharma
  • 715
  • 1
  • 15
  • 24