How to connect mongo db with Laravel and setting up mongo db with xamp also included
Hello, I hope you guys are doing well, so let's begin the integration part
How To Setup Mongo DB in Xampp step by step guide
step 1- https://pecl.php.net/package/mongodb visit this website and click on the dll, and download the zip as per your php version, see the image below
when you click on it choose the thread safe version to download see the image below
step 2 - then put the dll file on this path C:\xampp\php\ext your path might be different depending upon the xampp installation
step 3 - open your php.ini file and put this line extension=php_mongodb.dll after your extension=zip
**
if you have trouble opening php.ini file see the below image
Now from we will see how setup mongo with the laravel application
How to Connect and Perform CRUD Operation with MongoDB in Laravel: Complete Beginner Guide with Example
If you're looking to build scalable, high-performance Laravel applications with flexible schema support, MongoDB is a fantastic choice. In this tutorial, we'll walk you through how to perform CRUD (Create, Read, Update, Delete) operations in Laravel using MongoDB โ with no complex setup.
Weโll use a real-world example: a Product CRUD operation with MongoDB in Laravel, and provide a Bootstrap-based form UI for inserting data.
๐ง Why Use MongoDB with Laravel?
Before diving into code, let's understand the benefits of using MongoDB with Laravel:
- Flexible Schema: MongoDB is a NoSQL database, ideal for applications with dynamic data.
- High Scalability: Easily handles large volumes of data and high-traffic apps.
- JSON-Based Storage: MongoDB stores data in BSON (binary JSON), which aligns naturally with Laravel's JSON responses.
- Performance: MongoDB shines in read-heavy applications and real-time systems.
๐ฆ Step 1: Install MongoDB Package in Laravel
First, you need to install the official MongoDB Laravel package.
This package integrates Laravel with MongoDB, allowing you to use Eloquent ORM-style syntax.
โ๏ธ Step 2: Configure MongoDB Database in Laravel
Update .env
file:
๐ If you're using authentication with MongoDB, be sure to fill inDB_USERNAME
andDB_PASSWORD
.
Update config/database.php
:
In the connections
array, add:
Now, Laravel is ready to talk to MongoDB!
๐งฑ Step 3: Create Product Model (Using MongoDB)
Letโs create a simple Product
model for our CRUD example.
Run the command:
Edit the generated model at app/Models/Product.php
:
โ SEO Keyword Target: โLaravel MongoDB Eloquent model exampleโ
๐งโ๐ป Step 4: Create Controller for CRUD Operations
Now letโs build the logic for inserting and retrieving products using MongoDB.
Create the controller:
Add the following code in ProductController.php
:
This simple controller allows you to:
- Create a new product
- Display a list of all products
๐ Step 5: Define Web Routes for Product CRUD
Open routes/web.php
and define your routes:
Now weโre ready to create the front-end views.
๐ฅ๏ธ Step 6: Create Blade Views with Bootstrap
Letโs keep it simple and clean with Bootstrap 5 for our product create form and listing.
resources/views/product-create.blade.php
resources/views/product-list.blade.php
Now go to:
/product/create
to add products/products
to list them
๐งช Step 7: Testing MongoDB CRUD in Laravel
After completing the above setup, hereโs what you can test:
- โ Add a product using the form
- ๐ View all products in the list
- ๐งผ Clear or reset the database via Mongo shell if needed
Below I have added the image of insertion with mongo db
If you're using MongoDB Compass, you'll be able to view your products
collection in real time.
๐ฏ Conclusion: CRUD Operation with MongoDB in Laravel
Youโve just built a complete MongoDB CRUD system in Laravel with a working frontend! This guide is perfect for developers who want a clean and simple way to use MongoDB with Laravel without diving into complex schemas or third-party ORMs.
MongoDB offers unmatched flexibility for modern Laravel projects โ from eCommerce to SaaS apps.