How To Connect Mongo Db With Laravel And Setting Up Mongo Db With Xamp Also Included

image
image
image
image
image
image
image
image
How to connect mongo db with Laravel and setting up mongo db with xamp also included

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:

  1. Flexible Schema: MongoDB is a NoSQL database, ideal for applications with dynamic data.
  2. High Scalability: Easily handles large volumes of data and high-traffic apps.
  3. JSON-Based Storage: MongoDB stores data in BSON (binary JSON), which aligns naturally with Laravel's JSON responses.
  4. 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.

composer require mongodb/laravel-mongodb

This package integrates Laravel with MongoDB, allowing you to use Eloquent ORM-style syntax.

โš™๏ธ Step 2: Configure MongoDB Database in Laravel

Update .env file:

DB_CONNECTION=mongodb
MONGODB_URI="mongodb://localhost:27017"
MONGODB_DATABASE="laravel_app"
๐Ÿ” If you're using authentication with MongoDB, be sure to fill in DB_USERNAME and DB_PASSWORD.

Update config/database.php:

In the connections array, add:

'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('MONGODB_URI', 'mongodb://localhost:27017'),
'database' => env('MONGODB_DATABASE', 'laravel_app'),
],

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:

php artisan make:model Product

Edit the generated model at app/Models/Product.php:

<?php

namespace App\Models;

use MongoDB\Laravel\Eloquent\Model;

class Product extends Model
{
protected $connection = 'mongodb';
protected $collection = 'products';

protected $fillable = [
'name',
'price',
'description',
];
}

โœ… 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:

php artisan make:controller ProductController

Add the following code in ProductController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;

class ProductController extends Controller
{
public function create()
{
return view('product-create');
}

public function store(Request $request)
{
Product::create($request->only(['name', 'price', 'description']));
return redirect()->back()->with('success', 'Product added successfully!');
}

public function index()
{
$products = Product::all();
return view('product-list', compact('products'));
}
}

This simple controller allows you to:

  1. Create a new product
  2. Display a list of all products

๐ŸŒ Step 5: Define Web Routes for Product CRUD

Open routes/web.php and define your routes:

use App\Http\Controllers\ProductController;

Route::get('/product/create', [ProductController::class, 'create']);
Route::post('/product/store', [ProductController::class, 'store']);
Route::get('/products', [ProductController::class, 'index']);

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

<!DOCTYPE html>
<html>
<head>
<title>Add Product - MongoDB with Laravel</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-5">
<div class="container">
<h2>Add New Product</h2>

@if(session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif

<form method="POST" action="/product/store">
@csrf
<div class="mb-3">
<label>Name</label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="mb-3">
<label>Price</label>
<input type="number" name="price" class="form-control" required>
</div>
<div class="mb-3">
<label>Description</label>
<textarea name="description" class="form-control" required></textarea>
</div>
<button class="btn btn-primary">Save Product</button>
</form>
</div>
</body>
</html>

resources/views/product-list.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Product List - CRUD Operation with MongoDB</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-5">
<div class="container">
<h2>All Products</h2>
<table class="table table-bordered">
<tr>
<th>Name</th>
<th>Price</th>
<th>Description</th>
</tr>
@foreach($products as $product)
<tr>
<td>{{ $product->name }}</td>
<td>โ‚น{{ $product->price }}</td>
<td>{{ $product->description }}</td>
</tr>
@endforeach
</table>
</div>
</body>
</html>

Now go to:

  1. /product/create to add products
  2. /products to list them

๐Ÿงช Step 7: Testing MongoDB CRUD in Laravel

After completing the above setup, hereโ€™s what you can test:

  1. โœ… Add a product using the form
  2. ๐Ÿ“ƒ View all products in the list
  3. ๐Ÿงผ 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.