How to change the environment in Laravel 5.1?

Homo Sapien picture Homo Sapien · Jul 18, 2015 · Viewed 21k times · Source

What I understand about the working of environments in Laravel is that you have different environments for different, well environments. So, if you are running your app locally you could have a .env.local file. If you are testing or on production, you could use .env.testing or .env.production. (Correct me if I am wrong.)
By default we get .env file that we can edit. But can anybody tell me what is the workflow of changing the environments in Laravel. I tried the documentation but I couldn't get it. Please help me.

Answer

Mohamed Salem Lamiri picture Mohamed Salem Lamiri · Jul 18, 2015

When you install Laravel 5.1 you get two files .env and .env.example if you want to work locally you set :

APP_ENV=local
APP_DEBUG=true

in prod you set

APP_ENV=production
APP_DEBUG=false

An error message in debug mode

enter image description here

An error message from production mode

enter image description here

Note: you have two .env files .env and .env.example .. you can also create another one that you name .env.production but keep in mind that in order to get your configuration loaded you must just rename your file to .env

EDIT : So in case you are still working in local and you need another database for test, you can create another file so in total you have 3 .env files :

.env.production
.env.local1
.env.local2

whenever you want to switch configuration just rename the desired file to .env