How to Install and Setup MongoDB in Laravel 10

In the ever-evolving landscape of web development, staying current with the latest technologies is crucial. As a developer, I've always sought out ways to integrate powerful tools into my projects, and today, I'm excited to share my exploration of MongoDB, a NoSQL database, seamlessly integrated with Laravel 10, the latest iteration of the framework.

MongoDB offers unparalleled flexibility and scalability, making it an excellent choice for applications with complex data structures or rapidly changing requirements.

By harnessing the capabilities of MongoDB within Laravel 10, I can take advantage of its document-oriented storage and robust querying capabilities, all while benefiting from Laravel's elegant and developer-friendly features.

In this comprehensive guide, I will walk you through the process of installing, configuring, and connecting MongoDB to your Laravel 10 project.

Together, we'll explore each step with clear instructions, providing code examples and best practices along the way. Whether you're a seasoned Laravel developer or just starting your journey, this article will equip you with the knowledge and skills needed to harness the power of MongoDB within your Laravel 10 applications.

So, let's dive in and discover how to harness the synergy of Laravel 10 and MongoDB, opening up a world of possibilities for our web development projects.

Prerequisites

Laravel is a PHP framework, you will need to have PHP installed on your computer. The procedure may vary depending on your operating system. For more details on this step, please see the official installation documentation. For our walkthrough, you will need PHP version 7.4 installed on your computer.

The next step is to install Composer. Composer is a library and dependency manager for PHP, allowing you to add libraries that you may want to use in your project.

Step 1: Install Laravel

If you haven't already created a Laravel project, you can do so using Composer. Open your terminal and run the following command, replacing "project-name" with your desired project name.

composer create-project laravel/laravel project-name

 

Step 2: Install MongoDB PHP Extension

To interact with MongoDB in Laravel, you need to install the MongoDB PHP extension. You can do this using Composer. 

composer require jenssegers/mongodb

OR

composer require mongodb/laravel-mongodb

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

'providers' => [
    // ...
    MongoDB\Laravel\MongoDBServiceProvider::class,
],

This package adds functionalities to the Eloquent model and Query builder for MongoDB, using the original Laravel API. This package was renamed to mongodb/laravel-mongodb because of a transfer of ownership to MongoDB, Inc. It is compatible with Laravel 10.x.

 

Step 3: Configure MongoDB Connection

Next, you need to configure your MongoDB connection in Laravel. Open the .env file in your Laravel project and add the following MongoDB connection settings

MONGODB_CONNECTION=mongodb
MONGODB_HOST=127.0.0.1
MONGODB_PORT=27017
MONGODB_DATABASE=your_database_name
MONGODB_USERNAME=your_username
MONGODB_PASSWORD=your_password

To set up the MongoDB configuration in the config/database.php file in Laravel, you'll need to define a new connection configuration for MongoDB. Here's how you can do it.

  1. Open the config/database.php file in your Laravel project.

  2. Locate the 'connections' array within the file, which contains configurations for various database connections (e.g., MySQL, SQLite). You can add a new entry for MongoDB below these connections.

Here's an example of how to configure MongoDB in the database.php file:

'mongodb' => [
    'driver' => 'mongodb',
    'host' => env('MONGODB_HOST', '127.0.0.1'),
    'port' => env('MONGODB_PORT', 27017),
    'database' => env('MONGODB_DATABASE', 'your_database_name'),
    'username' => env('MONGODB_USERNAME', ''),
    'password' => env('MONGODB_PASSWORD', ''),
    'options' => [
        'database' => 'admin', // Default database for user authentication (if needed)
    ],
],

In this configuration:

  • 'driver' specifies the database driver as 'mongodb'.
  • 'host', 'port', 'database', 'username', and 'password' are the MongoDB connection details. These values should match the ones you set in your .env file, but they also have default values that can be used if the corresponding values are not found in the .env file.
  • 'options' can be used to specify additional configuration options. In this example, we set the default authentication database to 'admin', which is common when using authentication with MongoDB.

Now that your MongoDB connection is configured in database.php, you can use it in your Laravel application by specifying the connection name in your Eloquent models. For example:

protected $connection = 'mongodb';

With these steps, your Laravel application should be set up to use MongoDB as a database connection. Be sure to adjust the configuration to match your MongoDB server's details and authentication requirements

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS