DEV Community

RANA SAHA
RANA SAHA

Posted on

How to Setup MongoDB with Laravel (Quick Guide)

Modern web applications often need scalable and flexible data storage — and that’s where MongoDB fits perfectly.

It’s a NoSQL database that stores data in JSON-like documents, making it ideal for dynamic or unstructured data.

By integrating MongoDB with Laravel, you can enjoy both:

  • Laravel’s expressive Eloquent-like syntax, and
  • MongoDB’s flexible schema and high scalability.

This step-by-step guide explains how to connect a Laravel application with MongoDB using the official mongodb/laravel-mongodb package.

You’ll learn how to install dependencies, configure your environment, and test basic CRUD operations seamlessly.


⚙️ What You'll Learn

  • Install the MongoDB driver for Laravel
  • Configure your .env and database settings
  • Create models and run CRUD operations
  • Test your setup with Tinker
  • Troubleshoot connection issues

🧩 Prerequisites

  • PHP (compatible version for your Laravel installation)
  • Composer
  • A MongoDB instance (local or MongoDB Atlas)

1️⃣ Install the MongoDB Laravel Package

Run the following command in your Laravel project:

composer require mongodb/laravel-mongodb
Enter fullscreen mode Exit fullscreen mode

If you need to bypass the PHP extension check (not recommended for production):

composer require mongodb/laravel-mongodb --ignore-platform-req=ext-mongodb
Enter fullscreen mode Exit fullscreen mode

2️⃣ Configure Your .env

Update your database configuration in .env:

DB_CONNECTION=mongodb
MONGODB_URI="mongodb+srv://<username>:<password>@cluster0.mongodb.net"
MONGODB_DATABASE=laravel_mongo_demo
Enter fullscreen mode Exit fullscreen mode

Replace , , and the database name with your details.

3️⃣ Update config/database.php

Add or modify your MongoDB connection configuration:

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

4️⃣ Create a Model and Use It

Create a model called Post:

php artisan make:model Post
Enter fullscreen mode Exit fullscreen mode

Example usage:

// Create a new post
App\Models\Post::create([
    'title'  => 'First Post',
    'body'   => 'Testing MongoDB with Laravel.',
    'author' => 'Admin',
    'status' => 'published',
]);

// Fetch all posts
App\Models\Post::all();

Enter fullscreen mode Exit fullscreen mode

5️⃣ Quick Test with Tinker

Run Tinker to test the setup:

php artisan tinker
Enter fullscreen mode Exit fullscreen mode

Then try the example create/fetch commands above.


⚡ Optional — Install PHP MongoDB Extension

For better performance and full driver support, install the MongoDB PHP extension.

  1. Install via PECL or your package manager.
  2. On Windows (WAMP/XAMPP):
  • Download from PECL
  • Place the .dll file in your PHP ext folder (e.g. C:\wamp64\bin\php<php-version>\ext)
  • Add extension=mongodb to your php.ini
  • Restart your server and verify:
php -m | find "mongodb"
Enter fullscreen mode Exit fullscreen mode

🐞 Troubleshooting

  • If php artisan tinker errors occur, ensure the MongoDB extension is enabled.
  • Check your .env credentials and MongoDB Atlas IP whitelist.
  • Review logs at storage/logs/laravel.log for driver or connection issues.

✅ Conclusion

Integrating MongoDB with Laravel is simple and powerful using the mongodb/laravel-mongodb package.
With this setup, you can perform CRUD operations effortlessly while enjoying MongoDB’s flexibility and scalability.


🔗 Resources

📘 Full Blog Post:
👉 How to Setup MongoDB with Laravel (on Blogger)

💻 Source Code on GitHub:
👉 RanaGithub30/Setup-MongoDB-with-Laravel

Top comments (0)