From b8e8f2b6d29fbc5041e017856c194e33210bd941 Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Mon, 22 Apr 2024 23:12:50 -0400 Subject: [PATCH 1/3] DOCSP-38344: remove environment variables from database config file --- my-app/config/database.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/my-app/config/database.php b/my-app/config/database.php index 7da760d..2222e72 100644 --- a/my-app/config/database.php +++ b/my-app/config/database.php @@ -15,7 +15,7 @@ | */ - 'default' => env('DB_CONNECTION', 'mongodb'), + 'default' => env('mongodb'), /* |-------------------------------------------------------------------------- @@ -36,7 +36,7 @@ 'connections' => [ 'mongodb' => [ 'driver' => 'mongodb', - 'dsn' => env('DB_URI', ''), + 'dsn' => '', 'database' => 'sample_mflix', ], From a41817776a7058d6778f0cb13a5765aeaf3a5abe Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Tue, 23 Apr 2024 10:15:32 -0400 Subject: [PATCH 2/3] fix default connection --- my-app/config/database.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/my-app/config/database.php b/my-app/config/database.php index 2222e72..c3aae95 100644 --- a/my-app/config/database.php +++ b/my-app/config/database.php @@ -15,7 +15,7 @@ | */ - 'default' => env('mongodb'), + 'default' => 'mongodb', /* |-------------------------------------------------------------------------- From 3289858938ec8eb139c1b8674f56518eee03ac2c Mon Sep 17 00:00:00 2001 From: Chris Cho Date: Tue, 23 Apr 2024 12:14:45 -0400 Subject: [PATCH 3/3] reversal: use env variables instead --- README.md | 30 +++++++++++++++++++++++++----- my-app/config/database.php | 4 ++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f311dba..d3cce0f 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,14 @@ Run the following ``composer`` command to update your dependencies: composer update ``` +### Generate an App Key + +Run the following command to generate a Laravel application key: + +``` +php artisan key:generate +``` + ### Make a Copy of the Example Environment File Run the following command to use the example environment file @@ -55,18 +63,27 @@ in your sample app: cp .env.example .env ``` -### Generate an App Key +### Set the Default Connection Environment Variable -Run the following command to generate a Laravel application key: +Open the ``.env`` file in an editor and set the value of the ``DB_CONNECTION`` +variable to ``mongodb`` as shown in the following example: ``` -php artisan key:generate +DB_CONNECTION=mongodb ``` ### Configure your MongoDB Credentials -In the ``config/database.php`` file, replace the ```` placeholder -with your MongoDB connection string. +In the ``.env`` file, replace the value of the ``DB_URI`` variable with your +MongoDB connection string. + +For example, if your connection string is +"mongodb+srv://myUser:myPass123@mongo0.example.com/", +your ``DB_URI`` variable matches the following line: + +``` +DB_URI="mongodb+srv://myUser:myPass123@mongo0.example.com/" +``` If you need to create a MongoDB Atlas deployment, see the [Create a MongoDB Deployment](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/quick-start/create-a-deployment) @@ -76,6 +93,9 @@ If you already have a MongoDB Atlas deployment, see the [Create a Connection String](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/quick-start/create-a-connection-string) step of the Quick Start. +> [!NOTE] +> Make sure these variables in your ``.env`` file are undefined in the shell in which you run your application. Environment variables in the shell take precedence over the ones in the ``.env`` file. + ### Start the PHP Built-in Webserver To start the webserver, run the following command from the application root diff --git a/my-app/config/database.php b/my-app/config/database.php index c3aae95..5f94a47 100644 --- a/my-app/config/database.php +++ b/my-app/config/database.php @@ -15,7 +15,7 @@ | */ - 'default' => 'mongodb', + 'default' => env('DB_CONNECTION'), /* |-------------------------------------------------------------------------- @@ -36,7 +36,7 @@ 'connections' => [ 'mongodb' => [ 'driver' => 'mongodb', - 'dsn' => '', + 'dsn' => env('DB_URI'), 'database' => 'sample_mflix', ],