11

I tried to get the environment variable from the .env in my root with

Route::get('/test', function () {
    return "value is". getenv('APP_ENV');
});

and

Route::get('/test', function () {
    return "it is". env('APP_ENV');
});

It is in .env

APP_NAME=Laravel
APP_ENV=local

How can I get access to it?

LeBlaireau
  • 17,133
  • 33
  • 112
  • 192

7 Answers7

21

With Laravel, you should avoid environmental variables outside of your configuration files.

In your config files, you can use environmental variables, example in config/app.php:

'env' => env('APP_ENV', 'production'),

Then you can access this using the config helper: config('app.env').

This allows you to cache your configuration and still access these values, since env('APP_ENV') will no longer work once your config is cached. As docs mention:

"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."

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
10

use env('ENVKEY') Don't forget to clear cache sometime it cause because of cache.

php artisan config:clear
php artisan cache:clear
composer dump-autoload

For more info look at the doc

David Refoua
  • 3,476
  • 3
  • 31
  • 55
user7747472
  • 1,874
  • 6
  • 36
  • 80
  • Application cache wouldn't affect environmental variables, only config cache. – Devon Bessemer Aug 23 '18 at 13:51
  • maybe not, but something it doesn't work. There are many case for example u can check this https://stackoverflow.com/questions/43040967/accessing-laravel-env-variables-in-blade – user7747472 Aug 23 '18 at 13:52
  • That's fine, but the only thing relevant is your first command. I suggest you research the other two commands if you think they are relevant to this question. – Devon Bessemer Aug 23 '18 at 13:54
  • I do think it's relevant therefor I added so. Thank you for ur info though i ll research on it – user7747472 Aug 23 '18 at 13:59
  • thanks for this took me 1hr to figure it out. new for laravel config – zero8 Oct 28 '20 at 12:20
9
Route::get('/test', function () {
    return "it is".config('app.name');
});
Sunil Bind
  • 507
  • 1
  • 5
  • 18
2

just run this commands in cmd.

php artisan config:cache

then

php artisan config:clear

then

php artisan cache:clear

1

laravel provides a global helper function for this kind of task

$val = config('app.something');

you can also set new values with the following method

config(['app.something' => 'cat']);

reference

for your particular task it would be

$val = config('app.env');

or to determine the env globally

$environment = App::environment();

i hope this helps, have a nice one!

netzding
  • 772
  • 4
  • 21
1

As per the Laravel Documentation on Environment Configuration,

All of the variables listed in this file will be loaded into the $_ENV PHP super-global when your application receives a request. You may use the env helper to retrieve values from these variables.

So, it is possible to access the variable as

$_ENV['envKey'];
Sujan Gainju
  • 4,273
  • 2
  • 14
  • 34
1
 App::environment()

try this please

Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
Akbar Soft
  • 1,028
  • 10
  • 19