You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+45-17Lines changed: 45 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,17 +67,23 @@ You can connect to multiple servers or replica sets with the following configura
67
67
Eloquent
68
68
--------
69
69
70
-
Tell your model to use the MongoDB model and set the collection (alias for table) property. The lower-case, plural name of the class will be used for the collection name, unless another name is explicitly specified.
70
+
This package includes a MongoDB enabled Eloquent class that you can use to define models for corresponding collections.
71
71
72
72
use Jenssegers\Mongodb\Model as Eloquent;
73
73
74
-
class MyModel extends Eloquent {
74
+
class User extends Eloquent {}
75
+
76
+
Note that we did not tell Eloquent which collection to use for the `User` model. Just like the original Eloquent, the lower-case, plural name of the class will be used as the table name unless another name is explicitly specified. You may specify a custom collection (alias for table) by defining a `collection` property on your model:
77
+
78
+
use Jenssegers\Mongodb\Model as Eloquent;
75
79
76
-
protected $collection = 'mycollection';
80
+
class User extends Eloquent {
81
+
82
+
protected $collection = 'users_collection';
77
83
78
84
}
79
85
80
-
If you are using a different database driver as the default one, you will need to specify the mongodb connection name within your model by changing the `connection` property:
86
+
**NOTE:** Eloquent will also assume that each collection has a primary key column named id. You may define a `primaryKey` property to override this convention. Likewise, you may define a `connection` property to override the name of the database connection that should be used when utilizing the model.
81
87
82
88
use Jenssegers\Mongodb\Model as Eloquent;
83
89
@@ -95,20 +101,17 @@ You may also register an alias for the MongoDB model by adding the following to
95
101
96
102
'Moloquent' => 'Jenssegers\Mongodb\Model',
97
103
98
-
This will allow you to use your registered alias like:
104
+
This will allow you to use the registered alias like:
99
105
100
-
class MyModel extends Moloquent {
101
-
102
-
protected $collection = 'mycollection';
103
-
104
-
}
106
+
class MyModel extends Moloquent {}
105
107
106
108
Query Builder
107
109
-------------
108
110
109
111
The database driver plugs right into the original query builder. When using mongodb connections, you will be able to build fluent queries to perform database operations. For your convenience, there is a `collection` alias for `table` as well as some additional mongodb specific operators/operations.
If you did not change your default database connection, you will need to specify it when querying.
@@ -125,6 +128,7 @@ The database driver also has (limited) schema builder support. You can easily ma
125
128
Schema::create('users', function($collection)
126
129
{
127
130
$collection->index('name');
131
+
128
132
$collection->unique('email');
129
133
});
130
134
@@ -137,27 +141,51 @@ Supported operations are:
137
141
- unique
138
142
- background, sparse, expire (MongoDB specific)
139
143
140
-
Read more about the schema builder on http://laravel.com/docs/schema
144
+
All other (unsupported) operations are implemended as dummy pass-through methods, because MongoDB does not use a predefined schema. Read more about the schema builder on http://laravel.com/docs/schema
145
+
146
+
Extensions
147
+
----------
141
148
142
-
Auth
143
-
----
149
+
### Auth
144
150
145
151
If you want to use Laravel's native Auth functionality, register this included service provider:
This service provider will slightly modify the internal DatabaseReminderRepository to add support for MongoDB based password reminders. If you don't use password reminders, you don't have to register this service provider and everything else should work just fine.
150
156
151
-
Sentry
152
-
------
157
+
### Sentry
153
158
154
159
If yo want to use this library with [Sentry](https://cartalyst.com/manual/sentry), then check out https://github.com/jenssegers/Laravel-MongoDB-Sentry
155
160
156
-
Sessions
157
-
--------
161
+
### Sessions
158
162
159
163
The MongoDB session driver is available in a separate package, check out https://github.com/jenssegers/Laravel-MongoDB-Session
160
164
165
+
Troubleshooting
166
+
---------------
167
+
168
+
### Class 'MongoClient' not found in ...
169
+
170
+
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.
171
+
172
+
To check if you have installed the driver correctly, run the following command:
173
+
174
+
$ php -i | grep 'Mongo'
175
+
MongoDB Support => enabled
176
+
177
+
### Argument 2 passed to Illuminate\Database\Query\Builder::__construct() must be an instance of Illuminate\Database\Query\Grammars\Grammar, null given
178
+
179
+
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\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:
0 commit comments