6

In staging and production environment, when I try to get my custom defined variable from .env file, it returns null. I have tried creating new Key for APP and cleared all sort of caches, but result is same.

APP_NAME="App - Staging"
APP_ENV=staging
APP_KEY=<HIDDEN_KEY>
APP_DEBUG=true
APP_LOG_LEVEL=none
APP_URL=https://staging.app.com

APP_BASE_URL="https://app-staging.app.com"

Clearing Cache

php artisan config:clear                     
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan config:cache

But when using following in blade view gets nothing

env('APP_BASE_URL') returns null
danyal14
  • 367
  • 1
  • 4
  • 18

3 Answers3

8

It is because you have run php artisan config:cache. If you are using config:cache, your env() calls should only be made in your config files.

See here: https://laravel.com/docs/5.6/configuration#configuration-caching

pseudoanime
  • 1,547
  • 12
  • 19
6

This post solved my issue, I defined custom config in config/app.php https://laracasts.com/discuss/channels/laravel/accessing-custom-environment-variable

/*
|-------------------------------------------------------------------------
| Custom config variables
|-----------
|
 */
'base_url' => env('APP_BASE_URL', 'http://localhost'),

Then in .env I definded:

APP_BASE_URL="https://app-staging.app.com"

Finally cleared the cache, worked.

In blade view

{{ config('app.base_url') }}
danyal14
  • 367
  • 1
  • 4
  • 18
3

Yes it will return only null

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.

See the laravel documentation https://laravel.com/docs/5.6/configuration#configuration-caching

Parithiban
  • 1,656
  • 11
  • 16
  • so, lets say in production I run php artisan config:cache, and in blade view env('APP_BASE_URL') is called it return null. then what is the way of retrieving APP_BASE_URL from config then? – danyal14 Jun 27 '18 at 21:07