How can I save changes in .env file in Laravel? - laravel

I have a huge problem with .env file in Laravel. When I make a change in .env file, it is not changed. I think all of you know what I'm talking about. I run this php artisan config:cache, but yet, nothing changed! I echo out a variable in .env file and nothing changed. What can I do for that?

When you cache the configuration all calls to env will return null because it does not load the .env any more. You should not have any calls to env outside of the config files.
You use the configuration system to get the values you want, which is why the config files make calls to env to get values from the environment.

If you using php artisan serve you have to reload it after changing .env file

Related

Laravel env('APP_URL') is not returning the correct value for localhost

When trying to retrieve the APP_URL from the Laravel config it returns the wrong URL for development only.
My env file has the following:
APP_URL=http://127.0.0.1:9000
However when I call env('APP_URL') it returns me:
'http://localhost'
Which will not work with my current docker set-up it has to be 127.0.0.1:9000
In my config/app.php file I have the following:
'url' => env('APP_URL')
I have tried php artisan config:cache and php artisan config:clear but I still get the same result of http://localhost
Any ideas on where it could be getting http://localhost from other than the .env or config/app.php?
Thought it would be worth noting using config('app.url') also returns http://localhost
I found out there is an undocumented feature when loading .env files. If you have a .env.dev file in your project folder it will load this config over everything else if your application is in dev mode. It's documented that .env.testing is used in testing which makes sense but it also does it for dev with .env.dev
The application I was working on had a .env.dev and it was this file that had the http://localhost string inside of it.
Just in case anyone is having this issue in production, these are the steps that worked for me:
Make sure that the .env file is specifying that the app is in production. APP_ENV=production
Run php artisan optimize:clear
"localhost" is probably the default value. You error indicates that the .env file is not read at all. Make sure you're not editing .example.env, check if other env variables are accessible, check for typos, check file permissions on .env and if it's located in the root folder of the project.

Laravel - Why laravel ask to rename .env.example to .env while .env already exist?

In Laravel 6 Documentation second paragraph of Application Key written like this
Typically, this string should be 32 characters long. The key can be
set in the .env environment file. If you have not renamed the
.env.example file to .env, you should do that now. If the application
key is not set, your user sessions and other encrypted data will not
be secure!
Why did they ask to rename it? I also found this paragraph in the Laravel older version. Is there any difference between those since they have the same content but different name?
If you've install Laravel using composer command.
composer create-project laravel/laravel projectname
you don't need renamed the .env.example file to .env. If you installed Laravel via Composer or the Laravel installer, this key has already been set for you by the php artisan key:generate command.
If you clone project using git clone some folder is ignored by git so you might not get env file as well as vendor folder. Therefore, they will have to manually enter php artisan key:generate for their app to function correctly.
More info SO answer
Laravel need to use .env file to define a database connection, some general setting like application key as well. So if you have no .env file, your Laravel has not a setting for now.
Like they said, If the application key is not set, your user sessions and other encrypted data will not be secure! You need to create / copy /remove the .env.example to the new .env for this reason. for letting our Laravel knows about general config.
By the way, do not use the .env.example like copy-and-paste because it's an example. you need to change the value config to your own.
The simplest way is move it on your server with Filezilla or another FTP program. Rename file, and re-download it on your computer. It works for me last time :)
The .env.example the file is just an example of the .env file. It is not used by the app. It is used to serve as a base for you to edit and rename.
In a fresh Laravel installation, the root directory of your application will contain a .env.example file. If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually.

Deleted config.php and now site is returning HTTP error 500

We lost our config.php file and the site instantly broke. We tried putting an empty config.php file but no luck. Restarted the server but also no luck. What's strange is that we didn't have a config.php file earlier that day. Our live app is running without a config.php as well.
To clarify, this file resides in /bootstrap/cache/config.php
Thanks!
You can try to run php artisan config:clear from the command line to clear the config cache or php artisan config:cache to create a new config.php. Note that you need to use config() instead of env() in your code if you want to use config cache

getenv not working in EC2 Server

I'm trying to deploy to production my app that is working well in local.
Thing is when try:
dd(getenv('APP_ENV'));
it returns "false"
but when I connect ssh and type:
php artisan env
I get
production
Any idea why it stopped working???
For the record, In production, my deploy script execute 3 commands:
composer dump-autoload -o
php artisan route:cache
php artisan config:cache
I mention it because it is possibly the only software config that is different.
EDIT: I identify that the problematic command is:
php artisan config:cache
If if do:
php artisan config:clear
problem is solved.
Tx!
When using a cached config the .env file is not used anymore so getenv is useless, because the config is loaded from:
bootstrap/cache/config.php
Instead you can get the current environment from the loaded application configuration like so:
config('app.env');
Or directly using the app helper function:
app('env');
As a third option you can always use the environment method to get the current environment:
app()->environment(); // or App::environment()
Laravel uses the dotenv library internally to load the configuration keys from the .env file and add them to the environment variables using putenv, but when you cache your configuration that loading part is not done anymore because Laravel detects that there is a cache file present and uses that instead, so those keys from .env are not loaded into the environment, this not accessible via getenv.
And because configuration values from the .env file are only cached when they are used in an actual configuration file from the config directory, you need to create a configuration option for them to be cached and accessible when you're using the cache.
So if you want to have a BASE_URL key in your .env file with this value:
BASE_URL=http://domain.com/
If you want to be able to access its value when the configuration is cached, you need to use it in a configuration file. For example, you can add it to your config/app.php file like so:
'base_url' => env('BASE_URL')
Then you can access even when the configuration iit using:
config('app.base_url')
You can read more about accessing configuration values in the Laravel Documentation.

It's possible re-read .env file on Laravel 5.X

I needs to work with .env file on laravel.
But on some circumstances I need put a new file (.env) with new values, and I need update all environment vars and best way it's read .env file.
I don't see how to re-read .env file on Laravel (read for re-assign values to all env vars)

Resources