You are looking for a clean, fast way to ship a Laravel MongoDB CRUD Operation. You’re in the right place. In this laravel 12 mongodb tutorial, you’ll install MongoDB’s official Laravel package, connect your app, and build a complete CRUD API that feels as natural as Eloquent.
What is MongoDB?
MongoDB is a document database: it stores data as flexible, JSON-like documents. You can move fast without worrying about rigid schemas, evolve fields as your app grows, and scale horizontally as traffic increases.
Content platforms store posts and tags, without rigid migrations getting in the way. Marketplaces keep product attributes flexible as catalogs change. Event tracking apps capture dynamic payloads without schema rewrites.
Why choose MongoDB with Laravel?
Laravel provides you with expressive routing, validation, and a smooth developer experience, while MongoDB adds flexible documents, powerful indexing, and scale. Together, they deliver the high-speed stack for modern APIs.
- Flexible schema: Add fields as you learn from users.
- Speed at scale: Indexes and sharding handle growth.
- Familiar Eloquent-like API: The official package keeps your Laravel flow intact.
- Easy mongodb integration: Minimal config, maximum payoff.
Package Installation and Database Setup
Before moving further with implementation, you need to install official library from MongoDB website using composer into your project. Open terminal and enter below command to install MongoDB wrapper into Laravel.
composer require mongodb/laravel-mongodbIt will take some time to download and install into your Laravel application.
Configure environment
Next, you need to configure your database to work with MongoDB database. Modify this environment variables.
DB_CONNECTION=mongodb
DB_DSN=mongodb+srv://username:password@cluster0.xxxxx.mongodb.net
DB_DATABASE=laravel_appIt needs to change this DNS for database connection. However, you can also use local database but you need to install MongoDB into your local System. For Ubuntu, you can follow our guide on Install and Configure MongoDB on Ubuntu.
There is one more thing we have to change is database configuration. Add a mongodb connection and set it as default if this app uses only MongoDB.
<?php
return [
'default' => env('DB_CONNECTION', 'mongodb'),
'connections' => [
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_DSN'),
'database' => env('DB_DATABASE'),
],
],
];Build the CRUD: MongoDB CRUD in Laravel
We’ll create a simple Posts API with title, body, tags, and published_at. This laravel mongodb example mirrors a typical content workflow.
Let’s start with creating model and controller for our example. Enter below artisan command to create model and migration.
php artisan make:model Post
php artisan make:controller PostController --apiLet’s modify our model for supporting MongoDB connection and define our model properties.
<?php
namespace App\Models;
use MongoDB\Laravel\Eloquent\Model;
class Post extends Model
{
protected $connection = 'mongodb';
protected $collection = 'posts';
protected $fillable = [
'title',
'body',
'tags',
'published_at',
];
protected $casts = [
'published_at' => 'datetime',
];
}Here, connection property contains mongodb and posts collection.
Add API routes
Let’s add API routes for our post API endpoints. Open routes/api.php and modify it as below:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::apiResource('posts', PostController::class);Implement the controller
Let’s add modify out controller code which will handle CRUD operation using MongoDB as database. For basic CRUD API, we will allow user to Create, list, update and delete record from MongoDB collection. Open app/Http/Controllers/PostController.php and modify as below:
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index(Request $request)
{
$query = Post::query();
if ($request->filled('tag')) {
$query->where('tags', $request->string('tag'));
}
if ($request->filled('search')) {
$query->where('title', 'like', '%'.$request->string('search').'%');
}
return response()->json($query->orderByDesc('_id')->paginate(10));
}
public function store(Request $request)
{
$data = $request->validate([
'title' => 'required|string|max:120',
'body' => 'required|string',
'tags' => 'array',
'tags.*' => 'string',
'published_at' => 'nullable|date',
]);
$post = Post::create($data);
return response()->json($post, 201);
}
public function show(string $id)
{
$post = Post::findOrFail($id);
return response()->json($post);
}
public function update(Request $request, string $id)
{
$data = $request->validate([
'title' => 'sometimes|string|max:120',
'body' => 'sometimes|string',
'tags' => 'sometimes|array',
'tags.*' => 'string',
'published_at' => 'nullable|date',
]);
$post = Post::findOrFail($id);
$post->fill($data)->save();
return response()->json($post);
}
public function destroy(string $id)
{
$post = Post::findOrFail($id);
$post->delete();
return response()->json(null, 204);
}
}It lists posts with optional tag and keyword filters, creates new posts, fetches one, updates it, and deletes it. It validates each request, uses the Post model to talk to MongoDB, and returns clean JSON with proper HTTP status codes.
Conclusion
In this tutorial, you’ve just built a lightning-fast CRUD application with Laravel 12 using the official MongoDB package. The setup is dead simple, the code is clean, and the experience feels native to Laravel. And there you have it, speed, flexibility, and scale in one stack-that’s what this Laravel MongoDB CRUD Operation approach delivers. Extend the Post model, sprinkle in some indexes as your data grows, and ship features without schema drama.

