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.
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
An error message from production mode
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:)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With