How to change the environment in Laravel 5.1? How to change the environment in Laravel 5.1? laravel laravel

How to change the environment in Laravel 5.1?


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

APP_ENV=localAPP_DEBUG=true

in prod you set

APP_ENV=productionAPP_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


The idea of having .env.local.php, .env.production.php has been deprecated since Laravel 5. Now, in L5, we have single .env file, where you store all your environment configuration. To define your environment, you should put APP_ENV=local to this file.

Once you deploy your project on production, you would create .env file on the server and define APP_ENV=production

If you use service like Laravel Forge, it provides you nice simple way of storing your environment data. But that's another story:)

Edit

to make use of several db connections you might do the following:

in your config/database.php file

<?phpreturn array('default' => env('DEFAULT_DB_CONNECTION', 'mysql'),'connections' => array(    # Our primary database connection    'mysql' => array(        'driver'    => 'mysql',        'host'      => 'host1',        'database'  => 'database1',        'username'  => 'user1',        'password'  => 'pass1'        'charset'   => 'utf8',        'collation' => 'utf8_unicode_ci',        'prefix'    => '',    ),    # Our secondary database connection    'another_mysql' => array(        'driver'    => 'mysql',        'host'      => 'host2',        'database'  => 'database2',        'username'  => 'user2',        'password'  => 'pass2'        'charset'   => 'utf8',        'collation' => 'utf8_unicode_ci',        'prefix'    => '',    ),),

);

And then, in the .env file put another key

DEFAULT_DB_CONNECTION=another_mysql

Of course, this sort of predefines your connection. If you want to be dynamic, you can do the following

$users = DB::connection('another_db_connection')->select('users somehow');

that way you would get results from your secondary mysql connection, no matter what is set up in your environment