What I typically do is open /bootstrap/start.php and add
$env = $app->detectEnvironment(function () {
if (isset($_SERVER['APP_ENVIRONMENT']))
return $_SERVER['APP_ENVIRONMENT'];
elseif (file_exists( __DIR__ . '/environment.php'))
return include __DIR__ . '/environment.php';
});
Afterwards, I add /bootstrap/environment.php to the .gitignore.
Then, I can define the environment by setting a server var or by creating a /bootstrap/environment.php file that simply returns the environment I want to use.
On my local machine, I normally just create an environment.php.
Furthermore, since it's an if/elseif, if neither are defined it will default to production. That way I don't have to bother with setting any more server vars or anything when pushing it to the server.
I don't know if this is the best way, but it's how I do it and I like this approach.
To address your 2nd question, I do what Jeffrey does in many of his videos. I change /app/database.php to
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Then I create a /.env.local.php and a /.env.testing.php file that returns an array like so:
return [
'DB_HOST' => 'localhost',
'DB_NAME' => 'testing',
'DB_USERNAME' => 'root',
'DB_PASSWORD' => '',
];
The .env.*.php files should already be added to your .gitignore, so you don't have to worry about them. On the server, you'll have to setup the database using server vars or a /.env.production.php file.