Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.6 env('APP_BASE_URL') returns null

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
like image 836
danyal14 Avatar asked Jun 27 '18 11:06

danyal14


3 Answers

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

like image 84
pseudoanime Avatar answered Oct 19 '22 05:10

pseudoanime


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') }}
like image 40
danyal14 Avatar answered Oct 19 '22 04:10

danyal14


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

like image 2
Parithiban Avatar answered Oct 19 '22 05:10

Parithiban