Make sure you have the MongoDB PHP driver installed. You can find installation instructions at http://php.net/manual/en/mongodb.installation.php.php
Make sure you have the MongoDB PHP driver installed. You can find installation instructions at http://php.net/manual/en/mongodb.installation.php.php
For Laravel 5, install the latest stable version using composer:
**WARNING**: The old mongo PHP driver is not supported anymore in versions >= 3.0.
Installation using composer:
```
```
composer require jenssegers/mongodb
composer require jenssegers/mongodb
```
```
### Version Compatibility
### Laravel version Compatibility
Laravel | Package
Laravel | Package
:---------|:----------
:---------|:----------
4.2.x | 2.0.x
4.2.x | 2.0.x
5.0.x | 2.1.x
5.0.x | 2.1.x
5.1.x | 2.2.x
5.1.x | 2.2.x or 3.0.x
5.2.x | 2.2.x or 3.0.x
And add the service provider in `config/app.php`:
And add the service provider in `config/app.php`:
...
@@ -73,31 +76,31 @@ Change your default database connection name in `app/config/database.php`:
...
@@ -73,31 +76,31 @@ Change your default database connection name in `app/config/database.php`:
And add a new mongodb connection:
And add a new mongodb connection:
```php
```php
'mongodb'=>array(
'mongodb'=>[
'driver'=>'mongodb',
'driver'=>'mongodb',
'host'=>env('DB_HOST','localhost'),
'host'=>env('DB_HOST','localhost'),
'port'=>env('DB_PORT',27017),
'port'=>env('DB_PORT',27017),
'database'=>env('DB_DATABASE',''),
'database'=>env('DB_DATABASE',''),
'username'=>env('DB_USERNAME',''),
'username'=>env('DB_USERNAME',''),
'password'=>env('DB_PASSWORD',''),
'password'=>env('DB_PASSWORD',''),
'options'=>array(
'options'=>[
'db'=>'admin'// sets the authentication database required by mongo 3
'db'=>'admin'// sets the authentication database required by mongo 3
)
]
),
],
```
```
You can connect to multiple servers or replica sets with the following configuration:
You can connect to multiple servers or replica sets with the following configuration:
```php
```php
'mongodb'=>array(
'mongodb'=>[
'driver'=>'mongodb',
'driver'=>'mongodb',
'host'=>array('server1','server2'),
'host'=>['server1','server2'],
'port'=>env('DB_PORT',27017),
'port'=>env('DB_PORT',27017),
'database'=>env('DB_DATABASE',''),
'database'=>env('DB_DATABASE',''),
'username'=>env('DB_USERNAME',''),
'username'=>env('DB_USERNAME',''),
'password'=>env('DB_PASSWORD',''),
'password'=>env('DB_PASSWORD',''),
'options'=>array('replicaSet'=>'replicaSetName')
'options'=>['replicaSet'=>'replicaSetName']
),
],
```
```
Eloquent
Eloquent
...
@@ -135,7 +138,7 @@ class MyModel extends Eloquent {
...
@@ -135,7 +138,7 @@ class MyModel extends Eloquent {
}
}
```
```
Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
Everything else (should) work just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
### Optional: Alias
### Optional: Alias
...
@@ -216,34 +219,6 @@ If you want to use this library with [Sentry](https://cartalyst.com/manual/sentr
...
@@ -216,34 +219,6 @@ If you want to use this library with [Sentry](https://cartalyst.com/manual/sentr
The MongoDB session driver is available in a separate package, check out https://github.com/jenssegers/Laravel-MongoDB-Session
The MongoDB session driver is available in a separate package, check out https://github.com/jenssegers/Laravel-MongoDB-Session
Troubleshooting
---------------
#### Class 'MongoClient' not found in ...
The `MongoClient` class is part of the MongoDB PHP driver. Usually, this error means that you forgot to install, or did not install this driver correctly. You can find installation instructions for this driver at http://php.net/manual/en/mongo.installation.php.
To check if you have installed the driver correctly, run the following command:
```sh
$ php -i | grep'Mongo'
MongoDB Support => enabled
```
#### Argument 2 passed to Illuminate\Database\Query\Builder::__construct() must be an instance of Illuminate\Database\Query\Grammars\Grammar, null given
To solve this, you will need to check two things. First check if your model is extending the correct class; this class should be `Jenssegers\Mongodb\Eloquent\Model`. Secondly, check if your model is using a MongoDB connection. If you did not change the default database connection in your database configuration file, you need to specify the MongoDB enabled connection. This is what your class should look like if you did not set up an alias and change the default database connection:
You can also perform raw expressions on the internal MongoCollection object. If this is executed on the model class, it will return a collection of models. If this is executed on the query builder, it will return the original response.
You can also perform raw expressions on the internal MongoCollection object. If this is executed on the model class, it will return a collection of models. If this is executed on the query builder, it will return the original response.