josecabrerajr7's avatar

Connecting to MongoDB with Lumen Framework

Hello, I am running Lumen in Homestead and MongoDB on my local machine and wondering how do I connect to MongoDb on my local machine. I get an error saying

Class 'MongoDB\Driver\Manager' not found

I am using jenssegers/laravel-mongodb and I also have the php mongo driver installed. Running PHP7.0, Here is my files:

userModel.php

<?php namespace App;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class userModel extends Eloquent {
protected $connection = 'mongodb';
protected $collection = 'DATABASE_USERS';
protected $primaryKey = '_id';
}
?>

UsersController.php

<?php
namespace App\Http\Controllers;
use App\userModel;
class UsersController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
}
public function index() {
return userModel::all();
}
}

database.php

<?php
return  [
'default' => 'mongodb',
'connections' => [
'mysql' => [
'driver'    => 'mysql',
'host'      => env('DB_HOST', 'localhost'),
'database'  => env('DB_DATABASE', ''),
'username'  => env('DB_USERNAME', ''),
'password'  => env('DB_PASSWORD', ''),
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',
'strict'    => false,
],

            'mongodb' => array(
                    'driver'   => 'mongodb',
                    'host'     => env('MONGODB_HOST', 'localhost'),
                'port'     => env('MONGODB_PORT', 27017),
                    'username' => env('MONGODB_USERNAME', ''),
                'password' => env('MONGODB_PASSWORD', ''),
                    'database' => env('MONGODB_DATABASE', 'DATABASE_USERS'),
                    'options' => array(
                    'db' => env('MONGODB_AUTHDATABASE', '') 
                    )
            ),
    ],
];

?>

I also have the settings that is required in bootstrap/app.php and calling the index() function in routes.

0 likes
3 replies
elfeffe's avatar

Are you sure you have correct mongo driver installed? Because there is two, mongo and mongodb

willvincent's avatar

Lumen in Homestead and MongoDB on my local machine

You're trying to connect to the mongo server on your local machine from inside a VM? Then you'll need to use a different IP as localhost would literally be just the VM, but mongo is running on the host machine, not the guest.

But I'm curious.. why are you not running mongo inside the VM?

1 like

Please or to participate in this conversation.