Install MongoDB on Laravel Project — Jenssegers/MongoDB

Today I will show you how to using MongoDB database instead of using mySQL. By default since this story created, we need to manually installing MongoDB driver for laravel, and I will use jenssegers repository and install it by my own way. I’m using laravel version 5.6.* here by the way.

Step 1: Installing the repo to our project

Go to your project directory, in my case it is:

Next, we need to install the jenssegers/mongodb using composer:

composer require jenssegers/mongodb

Step 2: Add the service provider

Edit your config/app.php, and add this to your service providers:

Jenssegers\Mongodb\MongodbServiceProvider::class

Step 3: Setting Up Database configuration

Next, we need to set the database configuration in config/database.php. And putting this code:

‘default’ => env(‘DB_CONNECTION’, ‘mongodb’),

And these codes as well: (there are 2 options for the database configuration)

configuration, alternative 1

or this one, configuration, alternative 2:

configuration, alternative 2

for the second one, you can learn the DSN/URI here: https://docs.mongodb.com/manual/reference/connection-string/

Step 4: Config the .env File

Open up .env file. For alternative 1 on Step 3, use this config:

alternative 1

For alternative 2, use this config:

alternative 2

Step 5: Replacing the eloquent

Instead of using Illuminate eloquent by laravel, change and use the eloquent from jenssegers/mongodb:

— Completed —

Your laravel project using MongoDB by Jens Segers is ready to develope.

I wrote these tutorials for myself in future when I forget for the next steps.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store