Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the environment in Laravel 5.1?

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.

like image 531
Homo Sapien Avatar asked Jul 18 '15 09:07

Homo Sapien


2 Answers

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

like image 51
Mohamed Salem Lamiri Avatar answered Oct 08 '22 03:10

Mohamed Salem Lamiri


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

like image 33
Almazik G Avatar answered Oct 08 '22 02:10

Almazik G