MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web applications. Laravel is a powerful PHP framework known for its elegant syntax and rich ecosystem for building web applications. Combining Laravel with MongoDB provides a robust and efficient way for Laravel developers to build modern web applications.
Laravel developers can take advantage of MongoDB’s powerful features through the official MongoDB PHP driver and the mongodb/laravel-mongodb package. This package enables Laravel applications to use MongoDB with Eloquent, Laravel’s ORM, and provides integration for caching, queued jobs, and file storage with GridFS.
Contents
Use the below command to install the MongoDB PHP extension
pecl install mongodb
Verify extension using the below command
php -m | grep mongodb
MongoDB Community Server can run locally on Windows, macOS, and Linux, or in Docker containers. Follow the (https://www.mongodb.com/docs/manual/installation/) to get started.
Configure your .env file for local or cloud-hosted MongoDB:
MONGODB_URI="mongodb://localhost:27017" MONGODB_DATABASE="laravel_app"
To install the Laravel MongoDB package, use the following command:
composer require mongodb/laravel-mongodb
Configuration in Laravel
Go to config/database.php and add a new MongoDB Connection:
'connections' => [ 'mongodb' => [ 'driver' => 'mongodb', 'dsn' => env('MONGODB_URI', 'mongodb://localhost:27017'), 'database' => env('MONGODB_DATABASE', 'laravel_app'), ], ],
use Jenssegers\Mongodb\Eloquent\Model as Eloquent; class Post extends Eloquent { protected $connection = 'mongodb'; protected $collection = 'posts'; }
$posts = Post::where('status', 'published')->get();
Cache::put('key', 'value', 60);
You can queue jobs using MongoDB as a queue driver, enabling you to dispatch and process jobs efficiently.
MongoDB’s GridFS can store large files, such as images or videos, by breaking them into smaller chunks. Laravel’s GridFS Adapter integrates seamlessly with MongoDB for this purpose.
Storage::disk('gridfs')->put('file.txt', 'File content');
Define a Post model connected to MongoDB:
use Jenssegers\Mongodb\Eloquent\Model as Eloquent; class Post extends Eloquent { protected $connection = 'mongodb'; protected $collection = 'posts'; protected $fillable = ['title', 'content', 'status']; }
Set up a PostController to handle CRUD operations.
use App\Models\Post; class PostController extends Controller { public function create(Request $request) { $post = Post::create($request->all()); return response()->json($post); } public function read($id) { $post = Post::find($id); return response()->json($post); } public function update(Request $request, $id) { $post = Post::find($id); $post->update($request->all()); return response()->json($post); } public function delete($id) { Post::destroy($id); return response()->json('Post deleted successfully'); } }
Define routes for the CRUD operations in routes/web.php:
Route::post('/post', [PostController::class, 'create']); Route::get('/post/{id}', [PostController::class, 'read']); Route::put('/post/{id}', [PostController::class, 'update']); Route::delete('/post/{id}', [PostController::class, 'delete']);
By leveraging the features of Laravel and MongoDB, you can build flexible and scalable web applications. For any queries or doubts, share it with me through the comment box. Share the tutorial with your friends to help them learn about integrating MongoDB with Laravel.
Happy Coding!
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…
If you are a Shopify admin, using a Shopify Balance Account for your business revenue…
Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…