19

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.

Homo Sapien
  • 720
  • 2
  • 9
  • 19

3 Answers3

34

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

Mohamed Salem Lamiri
  • 5,767
  • 6
  • 34
  • 47
9

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

<?php
return 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

Almazik G
  • 1,066
  • 1
  • 9
  • 21
  • 2
    What if we are still working locally and we need another database configuration for testing. In that case what should we do? – Homo Sapien Jul 18 '15 at 09:47
  • Thank you very much for your help. I appreciate it. :) – Homo Sapien Jul 18 '15 at 10:38
  • @HomoSapien I'm sure you have already figured that out by this point :) but anyone looking for the answer - you now (in Laravel 6 and above) create a brand new `.env.testing` file in the project root that defines your settings specific to the testing environment – Almazik G Aug 26 '20 at 21:30
0

Another way to easily switch environment - is to force cache needed .env file. For example, I have the following .envs on my dev:

  • .env - for local development
  • .env.testing - for unit testing
  • .env.staging - for local testing with staging DBs

To quickly switch .env I just run command: php artisan config:cache --env=staging or with alias: art config:cache - this caches .env

DC Cat
  • 35
  • 2
  • 8