Complete Integration of Laravel Eloquent with MongoDB

Shantanu Bajaj
Ortigan
Published in
4 min readDec 28, 2023

INTRODUCTION

When it comes to web development, Laravel has become one of the most popular PHP frameworks. It provides a robust and elegant way to build web applications. One of the key components of Laravel is Eloquent, the ORM (Object-Relational Mapping) that allows developers to work with databases using PHP syntax. While Laravel Eloquent is primarily designed for relational databases like MySQL, it is also possible to integrate it with MongoDB, a NoSQL database.

In this article, we will explore the complete integration of Laravel Eloquent with MongoDB, providing you with step-by-step instructions to get started and the errors that are faced while doing so and how to tackle them.

Note: This article is written with respect to Windows OS and Laravel version 10.

PREREQUISITES

Before we dive into the integration process, there are a few prerequisites that you need to take care of:

INTEGRATION STEPS

Step-1: Create a Laravel 10 project and installation of mongo DB

To create a fresh laravel 10 project, go to your desired folder and simply run,

composer create-project laravel/laravel <your-project-name>

After creating the Laravel project, we will install the MongoDB package —

composer require mongodb/laravel-mongodb

You can refer to the documentation for installation https://github.com/mongodb/laravel-mongodb

After running the installation command, if you HAVE the php extension for MongoDB enabled in your system, the package will be installed without any issue, but if you DON’T then you will get the following error —

Step-2: Download the php ext-mongodb driver

We will download the PHP-MongoDB driver from the GitHub releases —

Note the version of PHP driver required in the error (1.15 in this case) above and download the driver accordingly.

The above image show the different variations of php driver 1.15. Now the question arises to many, including me, that which one of the version should be installed.

To tackle this, first check the current version of php that is being used by the system. You can check it by running the command-

php -version

In our case, it’s version 8.1.13. Now in the GitHub binary releases look for the php version after the driver version.

Now we download the version according to our Operating System, in this case we are using the 64-bit OS, so we’ll proceed with x64 zip. Now again the question arises what is ‘ts’ and ‘nts’. Well it refers to the thread safe and non-thread safe versions.

It’s preferred to use the nts version and we’ll proceed with that. To read more about the ‘ts’ and ‘nts’, refer the article —

Step-3: Setting up the mongodb driver

  • Go to the respective php folder and locate the ‘ext’ folder
  • Take the downloaded zip file of the driver that we did in Step-2 and extract the contents of that file into the ‘ext’ folder. If you get a pop up that files are already present, simply select to replace all files.
  • Next, we set up the php ini configuration file and enable the mongodb extension
  • Open the file, go to the end of file and simply add the following line —

extension=mongodb

Step-4: Run the installation command again

composer require mongodb/laravel-mongodb

Note: Make sure you close the previous terminal and run the command in the new one.

After running the command, if you are getting the error -

Then simply delete the ‘composer.lock’ file and run

composer update

and try the installation command again, it should work :)

CONCLUSION

These are the few steps that are required for the integration of MongoDB Eloquent ORM with Laravel 10. You can read the documentation of MongoDB given in Step-1 for further steps i.e. how to use the Eloquent model and the Laravel app configurations and env variables etc.

If you have any queries or issues feel free to leave a comment.

--

--

Ortigan
Ortigan

Published in Ortigan

A software development company and a multidisciplinary design studio.

Shantanu Bajaj
Shantanu Bajaj

Written by Shantanu Bajaj

Backend Engineer experienced in handling Laravel Applications