Accessing Laravel .env variables in blade
PhpLaravelPhp Problem Overview
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?
Php Solutions
Solution 1 - Php
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
Solution 2 - Php
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
Solution 3 - Php
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
Solution 4 - Php
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.
Solution 5 - Php
If you want to get the environment of the app then try this:
{{App::environment()}}
I have not tried other variables.
Solution 6 - Php
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.
Solution 7 - Php
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
Solution 8 - Php
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
Solution 9 - Php
get values here: config/app.php
in blade:
{{ config('app.name', 'default value here') }}
in class/controller:
config('app.name', 'default value here')
Solution 10 - Php
php artisan config:clear
should fix it
Solution 11 - Php
Run the following command.
php artisan optimize
Solution 12 - Php
This command should be written after you edit .env file to access variables in easy way
php artisan config:cache
Solution 13 - Php
i was also having trouble getting value from .env file, then i did this and it helped :
- Check env file and see if you have given the correct value.
- then check blade or controller where you using that variable from .env file.
- 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
Solution 14 - Php
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
return [ 'appenv' => 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
Solution 15 - Php
{{ env('APP_ENV') }}
**this is the write way.If doesn't work then run a command **
php artisan config:clear
Solution 16 - Php
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.