This article explains how to install and configure Laravel Package jenssegers/laravel-mongodb There are many MongoDB packages out there, but jenssegers Laravel MongoDB package (also called Moloquent) is the most popular among them. Moloquent is an Eloquent model and query builder based on MongoDB.
Pre-requisites
1. Install MongoDB driver for PHP
Check your PHP version using phpinfo(). Click here for instructions.
PECL has a collection of DLL files for different versions, architecture and thread safety. Choose the one matching your PHP installation.
In this example, the PHP installation is 5.6.17, 64 bit, Thread Safe (TS). So we go for driver version 1.1.8
Download the zip file containing the DLL.
Extract the archive and put php_mongodb.dll in your PHP extension directory ("ext" by default).
Add the following line to your php.ini file:
extension=php_mongodb.dll
Note: On earlier versions the dll file was called php_mongo.dll, but on newer versions it is called php_mongodb.dll
Restart Apache to load the module.
Check the output of phpinfo(). If the driver is installed correctly you should see a MongoDB section in your phpinfo() output as shown in the figure below.
In case you dont see the section,cross check the PHP version and architecture again.
2. Install Laravel Package for MongoDB
Install the latest stable version of the jenssegers/laravel-mongodb package, compatible with your version of Laravel.
To check your Laravel version, run the command php artisan --version
inside your Laravel Application folder.
Install using composer to get the matching version of the package.
Navigate to your Laravel folder and issue the command.
composer require jenssegers/mongodb
3. Register the Service Provider
Add MongoDB service provider in the Providers section of app.php file. It is located in <Laravel Application Folder>/config/app.php
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
......
......
......
Jenssegers\Mongodb\MongodbServiceProvider::class,
4. Configure database connection
You now need to include your MongoDB database connection information in /config/database.php file. Open this file in text editor and add the following lines in connections
'connections' => [ 'mongodb' => [ 'driver' => 'mongodb', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 27017), 'database' => env('DB_DATABASE', 'your_db_name'), 'username' => env('DB_USERNAME', 'your_username'), 'password' => env('DB_PASSWORD', 'your_password'), 'options' => [ 'database' => 'admin' // set the authentication database ] ],
You also need to change the default connection name to mongodb in /config/database.php file.
'default' => env('DB_CONNECTION', 'mongodb'),
5. Extend models from Moloquent
You can use the MongoDB enabled Eloquent class to define Models for your MongoDB collections. In the example below model class book is extended from the MongoDB enabled Jenssegers model instead of the default Eloquent class.
<?php
namespace App;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class book extends Eloquent
{
//
}
By default, this book model will use a MongoDB collection named books (plural of the class name). You may specify a different collection name to use by defining a collection property.
protected $collection = 'users_collection';