0
votes

i use different mongo db on both my production and development environment.

i am having different connection configurations because mongodb(production connection name) is on mongo atlas cluster so it connects using dsn however mongodb1 (development connection name) is installed locally so i connect it using host and port .

i want to use different db connections for different environments, so i have set DB_CONNECTION in env file different in both the cases. for my production is

     DB_CONNECTION=mongodb

and for development its

     DB_CONNECTION=mongodb1

in my config/database.php file
'default' => env('DB_CONNECTION', 'mysql'),

and in the connections array i have

    'mongodb' => [
        'driver' => 'mongodb',
        'dsn' => env('MONGO_DB_DSN'),
        'database' => env('MONGO_DB_DATABASE', 'uspl'),
        'username' => env('MONGO_DB_USERNAME', 'uspl'),
        'password' => env('MONGO_DB_PASSWORD', 'unfoldingskies'),
        'options' => array(
            'db' => 'uspl'
        )
    ],

    'mongodb1' => [
        'driver' => 'mongodb',
        'host' => env('MONGO_DB_HOST', 'localhost'),
        'port' => env('MONGO_DB_PORT', 27017),
        'database' => env('MONGO_DB_DATABASE'),
        'options' => []
    ],

i am getting error on my development environment -> Database [mongodb] not configured. in file
/var/www/html/devBackend/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php on line 152

i have tried php artisan config:clear, cache:clear, composer dump-autoload but nothing works

1

1 Answers

0
votes

Laravel does not support MongoDB out of the box, so you need to make use of a 3rd party package to use it.

As of this writing Laravel has the following database drivers:

  • MySQL 5.6+ (Version Policy)
  • PostgreSQL 9.4+ (Version Policy)
  • SQLite 3.8.8+
  • SQL Server 2017+ (Version Policy)

A quick search on the web got me this 3rd party package.

Laravel MongoDB

An Eloquent model and Query builder with support for MongoDB, using the original Laravel API. This library extends the original Laravel classes, so it uses exactly the same methods.