10

I added this variable to .env file

STRIPE_SECRET=a12345

I would like to dump the variable using routes/web.php

<?php
dd(env('STRIPE_SECRET'));

But it looks like it always returns null.

Update : Updated .env file. I only removed DB_PASSWORD.

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:08txDXXatyYsP5WQ4ECz35Q7OyBEe8Vgb/zK5fZsHik=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
APP_LOCALE=tr
APP_LC_ALL=tr_TR.utf8

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=gunluk
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=03ac580c85842d
MAIL_PASSWORD=1d6d902d296942
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

STRIPE=a123
STRIPE_SECRET=a12345
  • 4
    try runing following commands php artisan config:clear and php artisan optimize – Vision Coderz Sep 23 '17 at 16:42
  • Are you sure you have an app key? Run `php artisan key:generate` to make sure and then try again. – tptcat Sep 23 '17 at 16:54
  • After regenerating keys still it returns null. –  Sep 23 '17 at 16:57
  • your env not have STRIPE_SECRET but its STRIPE – Vision Coderz Sep 23 '17 at 17:13
  • Yes but tried to test whether it will dump STRIPE. It doesn't dump neither STRIPE nor STRIPE_SECRET. –  Sep 23 '17 at 18:03
  • Hmm...this issue doesn't make sense. Is this a new app? Or did something change recently? What OS runs on the server? – Cy Rossignol Sep 26 '17 at 06:46
  • 1
    Could you answer this questions: Where is placed your .env file? Have you tried to execute the `env("STRIPE")` call in tinker? The other env parameters works (like `env("APP_NAME")`? – Desh901 Sep 26 '17 at 10:13
  • @Desh901 my .env file resides in root of project. Output of `env("STRIPE")` or `env("APP_NAME")` is null when I execute this commands on php artisan tinker. –  Sep 26 '17 at 10:55
  • 1
    So basically it could be the fact that the configuration is cached and env values ar not loaded from `.env` file. Always access yout env variables within you through the `config` method. To test that run only the command `php artisan config:clear` and retry the above tests – Desh901 Sep 26 '17 at 11:00
  • @Desh901 now it works as expected. May you reply the thread so that I may offer bounty points. –  Sep 26 '17 at 11:05
  • Done, happy to hear that – Desh901 Sep 26 '17 at 11:09
  • Thanks. It says you may award your bounty in 11 hours. –  Sep 26 '17 at 11:12
  • 1
    Possible duplicate of [Laravel 5.3 - env() always returns null](https://stackoverflow.com/questions/43243732/laravel-5-3-env-always-returns-null) – Yevgeniy Afanasyev Nov 20 '18 at 03:12

7 Answers7

20

The main reason upon your issue is that you are caching your configuration. When running php artisan config:cache you're storing your configuration in your cache, and the next time Laravel will boot up it won't read the .env file because it detects that the configuration has been stored in the cache. Environment file should be used only to setup configuration files and then to access the value you're looking for you should use ONLY the config method.

Let's assume that you have the file config/stripe.php that consists of this content:

<?php

return [
    'secret' => env('STRIPE_SECRET', '')
];

Once you run php artisan config:cache access this value using ONLY the syntax config('stripe.secret') through your application code. Every time you update your config files and your .env you need to run php artisan config:cache again.

Desh901
  • 2,633
  • 17
  • 24
17

First, there is no STRIPE_SECRET inside your .env file(As per before edit the question). So please make sure that your .env must have this variable. You should clear your configuration cache by executing following commands in the same order

php artisan config:cache
php artisan config:clear 

Laravel cache your configuration files so the execution become faster. So everytime when you change the configuration files on server, you should clear the cache.

Additionally you can run these commands also to clear the other caches

php artisan cache:clear   //for clearing the cache
php artisan view:clear    //for clearing the compiled views
php artisan route:clear   //for clearing the routes cache

You can also create the routes for these commands and call the commands from the code also as

Route::get('/cache-clear', function() {
    $exitCode = Artisan::call('cache:clear');
    echo "Cache Cleard: ".$exitCode;
});

Route::get('/view-clear', function() {
    $exitCode = Artisan::call('view:clear');
    echo "View Cleard: ".$exitCode;
});

Route::get('/route-cache', function() {
    $exitCode = Artisan::call('route:cache');
    echo "Route Cached: ".$exitCode;
});

Route::get('/route-clear', function() {
    $exitCode = Artisan::call('route:clear');
    echo "Route Cache Cleared: ".$exitCode;
});

Route::get('/config-cache', function() {
    $exitCode = Artisan::call('config:cache');
    echo "Config Cached: ".$exitCode;
});

Route::get('/config-clear', function() {
    $exitCode = Artisan::call('config:clear');
    echo "Config Cache Cleared: ".$exitCode;
});
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
11

if you have STRIPE="a12345" this in .env file or if you any change in .env file or config file then you fallow these steps

one more thing write the variable value in comma's like STRIPE="a12345"

First run these commands

 1. php artisan config:clear
 2. php artisan cache:clear
 3. composer dump-autoload

and finally used this command to get variable

dd(env('STRIPE'));

this working for me

and also 1 stupid suggestion: restart server

I have add all possible solution

Salman Zafar
  • 3,844
  • 5
  • 20
  • 43
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
  • 1
    This worked for me. Tried steps 1 and 2 repeatedly (from other suggestions), but didn't work until I tried step 3 too. – user2796515 Aug 17 '18 at 13:18
3

You can clear configuration cache using following commands

php artisan config:clear 
php artisan optimize
php artisan config:cache

Also make sure

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application. If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

Reference link:

Update 1:

Also make sure you have renamed .env.example to .env file by default laravel have .env.example

Update 2

As per your new update question your env file have STRIPE not STRIPE_SECRET so you have access like this if its not typo error in question

env('STRIPE')
Salman Zafar
  • 3,844
  • 5
  • 20
  • 43
Vision Coderz
  • 8,257
  • 5
  • 41
  • 57
1

env(...) function will return null after you cached the config. (starting from laravel 5.2 till current 5.7)

The documentation says

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

So the correct answer would be to

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

And I quoted it from the same documentation

But for a quick fix this will do:

php artisan config:clear

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
  • Apart from running php artisan config:cache. If necessary, say I have API_DOMAIN=https://www.someaddress.com/ as a line in my .env file, which file do I put what line instead in? And what do I replace my env('API_DOMAIN') calls with? – Ben Kao Jan 14 '19 at 08:58
  • if you are lazy put it in `\config\app.php`, if you feel like you are making a new api service connector use `\config\services.php`, if you feel like it is a big thing - create your own file. – Yevgeniy Afanasyev Jan 15 '19 at 04:11
1

I would not recommend using the env('STRIPE') command to get a variable. In a loop, this command starts returning null after some iteration. Therefore, it is better to create a configuration file and use the command "config('file_name.variable_name)":

<?php
return [
    'sprite_secret' => env('STRIPE_SECRET', '')
];

Run these commands first

php artisan config:cache
php artisan config:clear

And use this command to get the value of the STRIPE_SECRET variable:

config('config_file.sprite_secret)
bakha
  • 311
  • 2
  • 8
0

make sure you app is completely booted.

if you are running server with cmd then try restarting server.

and if it not works try clearing the cache using the methods above mentioned by @iCoders.

Moeen Basra
  • 733
  • 4
  • 18