How to connect Laravel website with mongoDB atlas

Solution:

If you are using XAMPP for Localhost(Apache Server) and MongoDB Atlas as the Database you should refer this Jenssegers.

$ composer require jenssegers/mongodb

and make sure to install MongoDB extension to XAMPP XAMPP + MongoDB Configuration(Do Until Step 4)

.env

DB_CONNECTION=mongodb
DB_HOST=xxxx-shard-00-00-uggj0.mongodb.net
DB_PORT=27017
DB_DATABASE={your_database}
DB_USERNAME={username_when_creating_cluster}
DB_PASSWORD={password_when_creating_cluster}

config/database.php

   'mongodb' => [
            'driver' => 'mongodb',
            'dsn' => 'mongodb+srv://{username_when_creating_cluster}:{password_when_creating_cluster}@{your_cluster_name}.bhtvj.gcp.mongodb.net/{your_database}?retryWrites=true&w=majority',
            'database' => '{your_database}',
        ],

Please note that above dsn can easily get from MongoDB|Atlas and you should only edit credential for the cluster and the database name.

Step 1

 

Step 2

 

Step 3

 

Hope this is the answer you looking for Goodluck!