ColBatGuano's avatar

Setting laravel into dev mode?

I'm doing seeding on some of my tables and get a warning - "Application In Production!"

How do I set up Laravel so that it isn't in production?

On a related note how do I specify when I want to use my code, but on on a different database (for testing)?

0 likes
4 replies
chrio's avatar

The environment is set in bootstrap/start.php by env = $app->detectEnvironment(...)

Besides the docs @bashy mentioned, I would check out the easy environments cast.

Devon's avatar

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.

1 like

Please or to participate in this conversation.