10

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
  • 16,087
  • 30
  • 105
  • 176

7 Answers7

17

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.

Devon
  • 32,773
  • 9
  • 61
  • 91
  • Where you set the otherwise `'production'`, can it be a `'OTHER_ENV_VAR'` as well? – Pathros Jul 02 '20 at 18:23
  • 1
    I don't see why you couldn't nest env calls: `env('APP_ENV', env('OTHER_ENV_VAR', 'ultimate_default'))` – Devon Jul 02 '20 at 20:03
8
Route::get('/test', function () {
    return "it is".config('app.name');
});
Sunil Kumar
  • 467
  • 1
  • 4
  • 17
7

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

Formore info look at the doc

user7747472
  • 1,766
  • 5
  • 33
  • 75
  • Application cache wouldn't affect environmental variables, only config cache. – Devon 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 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
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
  • 732
  • 4
  • 18
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'];
suzan
  • 2,166
  • 2
  • 12
  • 26
1
 App::environment()

try this please

Jignesh Joisar
  • 11,406
  • 3
  • 52
  • 51