Laravel 5 - Env() Always Returns Null

Laravel 5 - env() always returns null

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

The Laravel 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

And now it should be clear why, when you tried config:cache, it did not help, even though it clears the config prior to caching.

Laravel .env variable always returns null

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.

Laravel 5.6 env('APP_BASE_URL') returns null

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

Laravel 5.0, env() returns null during concurrent requests

This is a know bug in dotenv package - see the discussion here
https://github.com/laravel/framework/issues/8191

env helper returns null even if providing default value

As long as the key exists in the .env file no matter if if is null or no value at all, that one will be used.

https://laravel.com/docs/6.x/configuration#retrieving-environment-configuration

The second value passed to the env function is the "default value". This value will be used if no environment variable exists for the given key.

Laravel env() value null

You should not use env() outside of the config files.

Read: https://laravel.com/docs/8.x/configuration

You should add the env variable to a config file and use config('example.url');.

The example.php would look like:

return [
'url' => env('EXAMPLE_URL', 'https://example.com'),
];

Store 'null' string in .env file Laravel 5

The anser to my question seems to be, no, you can't.

The solution was to use the second parameter of env('VAR_NAME','default').

The problem is that env() doesn't return the default value because VAR_NAME exists and have a value.

I comented VAR_NAME in the env file just in case I need to use it later.



Related Topics



Leave a reply



Submit