DEV Community

loading...
Cover image for the Best way to use Laravel with Nosql Mongodb

the Best way to use Laravel with Nosql Mongodb

Moataz khaled
・1 min read

Alt Text

1 -The first step we need to sure we installed Mongodb in the global environment on the PC.

2 - Installation

if ready I will be run in my project this command

 "$ composer require jenssegers/mongodb"
Enter fullscreen mode Exit fullscreen mode

3-Laravel

In case your Laravel version does NOT autoload the packages, add the service provider to config/app.php:

Jenssegers\Mongodb\MongodbServiceProvider::class,
Enter fullscreen mode Exit fullscreen mode

4- Configuration

You can use MongoDB either as the main database, either as a side database. To do so, add a new MongoDB connection to config/database.php:

     'mongodb' => [
        'driver' => 'mongodb',
        'host' => env('MONGO_DB_HOST','localhost'),
        'port' => env('MONGO_DB_PORT'),
        'database' => env('MONGO_DB_DATABASE'),
        'username' => env('MONGO_DB_USERNAME'),
        'password' => env('MONGO_DB_PASSWORD'),
        'options' => []
     ],
Enter fullscreen mode Exit fullscreen mode

if need to used DSN :

    'mongodb' => [
        'driver' => 'mongodb',
        'dsn' => env('MONGO_DB_DSN'),
        'database' => env('MONGO_DB_DATABASE'),
    ]
Enter fullscreen mode Exit fullscreen mode

5 - env File '.env'

     MONGO_DB_HOST=127.0.0.1
     MONGO_DB_PORT=27017
     MONGO_DB_DATABASE={my_db}
     MONGO_DB_USERNAME={my_user}
     MONGO_DB_PASSWORD={my_password}
Enter fullscreen mode Exit fullscreen mode

if need to used DSN :

    MONGO_DB_DSN= {my_link_dsn}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)