Accessing Laravel .env variables in blade

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?

151695 次浏览

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

php artisan config:clear

should fix it

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

php artisan config:cache

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

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

\{\{App::environment()}}

I have not tried other variables.

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

get values here: config/app.php


in blade:

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

in class/controller:

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

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

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

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.

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.

Run the following command.

php artisan optimize
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
\{\{ 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

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.