I added this variable to .env file
STRIPE_SECRET=a12345
I would like to dump the variable using routes/web.php
&
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 theenv
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
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.
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;
});
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
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)
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')