Install and configure Laravel with MongoDB on Windows

Install MongoDB driver for PHP:


Check your PHP version using phpinfo().
Download the zip file of dll which is suitable for your system configuration.
Extract the archive and put php_mongodb.dll in your PHP extension directory.
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.
MongoDB Driver
In case you don't see the section,cross-check the PHP version and architecture again.

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

Register the Service Provider

Add MongoDB service provider in the Providers section of the 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,

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', 'my_mongo'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DB_PASSWORD', 'root'),
    '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'),

Extend models from Eloquent:

You can use the MongoDB enabled Eloquent class to define Models for your MongoDB collections. In the example below model, the class student 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 Student extends Eloquent
 {
     //
 }
By default, this student model will use a MongoDB collection named students (plural of the class name). You may specify a different collection name to use by defining a collection property.


 protected $collection = 'users_collection';

Comments

Popular posts from this blog

Laravel 5 Chart example using Charts Package

Laravel Stats Tracker

PHPMyBackup - A PHP MySQL differential backup script