Using MongoDB into Laravel Application

In today’s digital era, managing large amounts of data is essential for building robust web applications. Laravel, a popular PHP framework, offers seamless integration with various databases, including MongoDB.MongoDB is a NoSQL database that provides scalability, flexibility, and excellent performance.

In this tutorial, we’ll explore how to use MongoDB in Laravel, highlighting its advantages and demonstrating a practical example to help you get started. Let’s dive in!

Why Choose MongoDB for Laravel

MongoDB stands out among traditional relational databases due to its document-oriented structure, making it ideal for projects with rapidly evolving requirements. Here are some key reasons why MongoDB is a great choice for Laravel developers:

  • Flexibility: MongoDB’s flexible document model allows you to store data with varying structures. It eliminates the need for rigid table structures found in traditional databases, making it easier to adapt to changing data requirements.
  • Scalability: MongoDB’s distributed architecture enables horizontal scaling, making it efficient in handling large amounts of data.
  • Performance: MongoDB’s powerful indexing capabilities and document-based querying allow for faster data retrieval.

Install and set up the MongoDB extension for PHP

Before starting, make sure you have PHP and Composer installed. Open your terminal and run the following command to install the MongoDB extension via PECL (PHP Extension Community Library):

sudo pecl install mongodb

It will command will download and install the MongoDB extension for PHP. Once installation is complete, we need to enable the MongoDB extension in your PHP configuration.

The location of the ini file can vary based on installation or operating system. To locate your PHP configuration file (php.ini) use the following command:

php --ini

Open the php.ini file at the above location and go to the extension section and add the following line to it.

At last, restart your PHP server using the below command.

// Apache2
sudo service apache2 restart

// nginx
sudo service nginx restart

If you are using XAMPP or WAMPP then restart it using a control panel.

Install and Configure MongoDB Package in Laravel

Begin by installing the MongoDB package for Laravel using Composer. Open your terminal and navigate to your Laravel project directory. Run the following command:

composer require jenssegers/mongodb

Once the package is installed, open the `config/database.php` file and modify the `connections` array to add a new MongoDB configuration. Specify the necessary details such as the database name, host, port, and authentication credentials.

'connections' => [
  'mongodb' => [
        'driver' => 'mongodb',
        'dsn' => env('DB_URI', 'mongodb+srv://username:password@<atlas-cluster-uri>/myappdb?retryWrites=true&w=majority'),
        'database' => 'myappdb',

Create a Model and Collection

Let’s create a model for our example. Here, we will create a Post model and associate MongoDB connection to it. Open your terminal and enter the below command to create Post model and migrations:

php artisan make:model Post -m

It will create App\Models\Post.php. By default, the artisan creates models that extend Illuminate\Database\Eloquent\Model. For using MongoDB we need to change it will our package’s model.

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;

class Post extends Model
   protected $connection = 'mongodb';


While storing data, Laravel will automatically create a collection in the MongoDB database.

To create a new post, use the below code:

$post = new Post;
$post->title = "New Post";
$post->content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

You can also use another Eloquent method like update, delete, find, etc.


In this tutorial, we explored the advantages of using MongoDB in Laravel for efficient data management. We also discussed MongoDB’s flexibility, scalability, and performance benefits. Additionally, we have taken a practical example of integrating MongoDB into a Laravel application, covering installation, configuration, and performing CRUD operations.