In modern web applications, developers often look for flexible and scalable database solutions — and that’s where MongoDB shines. Unlike traditional SQL databases, MongoDB stores data in a document-oriented format, making it ideal for projects that handle dynamic, unstructured, or rapidly evolving data. Integrating it with Laravel allows you to enjoy the power of MongoDB’s flexibility while still using Laravel’s elegant and expressive syntax.
This step-by-step guide walks you through the complete process of integrating
MongoDB with a Laravel application using the official
mongodb/laravel-mongodb package. You’ll learn how to install all required
dependencies, properly configure your .env and database settings, and
seamlessly perform basic CRUD (Create, Read, Update, Delete) operations
with MongoDB — all while maintaining Laravel’s familiar Eloquent-style syntax.
By the end, you’ll have a fully functional Laravel app connected to MongoDB,
ready for development or production use.
๐ View full project on GitHub
๐งฉ Prerequisites
- PHP (compatible with your Laravel version)
- Composer
- MongoDB instance (local or MongoDB Atlas)
⚙️ Step 1 — Install the MongoDB Laravel Package
Install the MongoDB driver for Laravel via Composer:
composer require mongodb/laravel-mongodb
If you must bypass the PHP extension check (not recommended for production):
composer require mongodb/laravel-mongodb --ignore-platform-req=ext-mongodb
๐งพ Step 2 — Configure the .env File
Set your database connection details:
DB_CONNECTION=mongodb
MONGODB_URI="mongodb+srv://<username>:<password>@cluster0.mongodb.net"
MONGODB_DATABASE=laravel_mongo_demo
Replace <username>, <password>, and cluster/database names as needed.
๐ Step 3 — Update config/database.php
Add or update the MongoDB connection settings:
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('MONGODB_URI'),
'database' => env('MONGODB_DATABASE'),
],
๐ Step 4 — Create a Model and Use It
Create a model named Post:
php artisan make:model Post
Example CRUD operations using Eloquent-style syntax:
// Create a new document
App\Models\Post::create([
'title' => 'First Post',
'body' => 'Testing MongoDB with Laravel.',
'author' => 'Admin',
'status' => 'published',
]);
// Fetch all documents
App\Models\Post::all();
๐ป Step 5 — Quick Test with Artisan Tinker
Open Tinker to test your model:
php artisan tinker
Run the example create/fetch commands above to verify the connection.
⚡ Optional — Install the PHP MongoDB Extension
For better performance and full driver support, install the mongodb PHP extension.
- Install via PECL or your system’s package manager.
- On Windows (WAMP/XAMPP):
- Download the correct MongoDB DLL from PECL.
- Place it in your PHP
extfolder (e.g.,C:\wamp64\bin\php\).\ext - Add
extension=mongodbinphp.ini. - Restart your server and verify with:
php -m | find "mongodb"
๐ Troubleshooting
- If
php artisan tinkerfails, ensure the MongoDB extension is enabled. - If connection fails, verify your
MONGODB_URIand Atlas network settings. - Check logs at
storage/logs/laravel.logfor driver issues.
๐ References
✅ Conclusion
Connecting MongoDB with Laravel is simple and efficient with the mongodb/laravel-mongodb package.
By configuring your environment and updating a few settings, you can start using MongoDB’s flexible schema and Eloquent-like features right away.
๐ Explore the full code here: Setup MongoDB with Laravel (GitHub)
Author: Rana Saha
Comments
Post a Comment