Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Configuring the `.env` file in Laravel

I want to configure both MySQL and MongoDB connections in my .env file in Laravel. How can I set the following parameters for both:

DB_CONNECTION=
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

in your .env:

# MySQL Database Configuration
DB_CONNECTION=mysql
MYSQL_DB_HOST=127.0.0.1
MYSQL_DB_PORT=3306
MYSQL_DB_DATABASE=my_database
MYSQL_DB_USERNAME=root
MYSQL_DB_PASSWORD=secret_password
# MongoDB Database Configuration
MONGO_DB_CONNECTION=mongodb
MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=my_mongo_database
MONGO_DB_USERNAME=mongo_user
MONGO_DB_PASSWORD=mongo_password

in config/database.php:

'mysql' => [
    'driver' => 'mysql',
    'host' => env('MYSQL_DB_HOST', '127.0.0.1'),
    'port' => env('MYSQL_DB_PORT', '3306'),
    'database' => env('MYSQL_DB_DATABASE', 'forge'),
    'username' => env('MYSQL_DB_USERNAME', 'forge'),
    'password' => env('MYSQL_DB_PASSWORD', ''),
],
'mongodb' => [
    'driver'   => 'mongodb',
    'host'     => env('MONGO_DB_HOST', 'localhost'),
    'port'     => env('MONGO_DB_PORT', 27017),
    'database' => env('MONGO_DB_DATABASE'),
    'username' => env('MONGO_DB_USERNAME'),
    'password' => env('MONGO_DB_PASSWORD'),
    'options'  => [
        'database' => 'admin', 
    ]
],

don’t forget to run php artisan config:cache after make these changes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading