@@ -188,16 +188,16 @@ Steps
188
188
189
189
use MongoDB\Laravel\Eloquent\Model;
190
190
191
- class CustomerMongoDB extends Model
192
- {
191
+ class CustomerMongoDB extends Model
192
+ {
193
193
// the selected database as defined in /config/database.php
194
- protected $connection = 'mongodb';
194
+ protected $connection = 'mongodb';
195
195
196
196
// equivalent to $table for MySQL
197
- protected $collection = 'laracoll';
197
+ protected $collection = 'laracoll';
198
198
199
199
// defines the schema for top-level properties (optional).
200
- protected $fillable = ['guid', 'first_name', 'family_name', 'email', 'address'];
200
+ protected $fillable = ['guid', 'first_name', 'family_name', 'email', 'address'];
201
201
}
202
202
203
203
.. step:: Perform CRUD operations.
@@ -208,12 +208,12 @@ Steps
208
208
209
209
.. code-block:: php
210
210
211
- Route::get('/create_eloquent_mongo/', function (Request $request) {
211
+ Route::get('/create_eloquent_mongo/', function (Request $request) {
212
212
$success = CustomerMongoDB::create([
213
213
'guid'=> 'cust_1111',
214
214
'first_name'=> 'John',
215
215
'family_name' => 'Doe',
216
-
216
+
217
217
'address' => '123 my street, my city, zip, state, country'
218
218
]);
219
219
});
@@ -223,7 +223,7 @@ Steps
223
223
224
224
.. code-block:: php
225
225
226
- Route::get('/find_eloquent/', function (Request $request) {
226
+ Route::get('/find_eloquent/', function (Request $request) {
227
227
$customer = CustomerMongoDB::where('guid', 'cust_1111')->get();
228
228
});
229
229
@@ -234,11 +234,11 @@ Steps
234
234
235
235
.. code-block:: php
236
236
237
- Route::get('/update_eloquent/', function (Request $request) {
237
+ Route::get('/update_eloquent/', function (Request $request) {
238
238
$result = CustomerMongoDB::where('guid', 'cust_1111')->update( ['first_name' => 'Jimmy'] );
239
239
});
240
240
241
- Route::get('/delete_eloquent/', function (Request $request) {
241
+ Route::get('/delete_eloquent/', function (Request $request) {
242
242
$result = CustomerMongoDB::where('guid', 'cust_1111')->delete();
243
243
});
244
244
@@ -262,26 +262,26 @@ Steps
262
262
263
263
.. code-block:: php
264
264
265
- Route::get('/create_nested/', function (Request $request) {
265
+ Route::get('/create_nested/', function (Request $request) {
266
266
$message = "executed";
267
267
$success = null;
268
268
269
- $address = new stdClass;
269
+ $address = new stdClass;
270
270
$address->street = '123 my street name';
271
271
$address->city = 'my city';
272
272
$address->zip= '12345';
273
273
274
274
275
275
try {
276
- $customer = new CustomerMongoDB();
276
+ $customer = new CustomerMongoDB();
277
277
$customer->guid = 'cust_2222';
278
278
$customer->first_name = 'John';
279
279
$customer->family_name= 'Doe';
280
280
$customer->email= $emails;
281
281
$customer->address= $address;
282
282
$success = $customer->save(); // save() returns 1 or 0
283
283
}
284
- catch (\Exception $e) {
284
+ catch (\Exception $e) {
285
285
$message = $e->getMessage();
286
286
}
287
287
return ['msg' => $message, 'data' => $success];
@@ -321,7 +321,7 @@ Steps
321
321
You can begin building a query from a ``collection`` object.
322
322
Eloquent exposes the full capabilities of the underlying database
323
323
by using "raw queries," which Laravel sends to the database
324
- without any processing from the Eloquent Query Builder.
324
+ without any processing from the Eloquent Query Builder.
325
325
326
326
You can perform a raw native MongoDB query from the model as shown
327
327
in the following code:
@@ -341,14 +341,14 @@ Steps
341
341
342
342
$mongodbquery = ['guid' => 'cust_1111', ];
343
343
$mongodb_native_collection = DB::connection('mongodb')->getCollection('laracoll');
344
- $document = $mongodb_native_collection->findOne( $mongodbquery );
345
- $cursor = $mongodb_native_collection->find( $mongodbquery );
344
+ $document = $mongodb_native_collection->findOne( $mongodbquery );
345
+ $cursor = $mongodb_native_collection->find( $mongodbquery );
346
346
347
347
The following code demonstrates multiple ways to perform queries:
348
348
349
349
.. code-block:: php
350
350
351
- Route::get('/find_native/', function (Request $request) {
351
+ Route::get('/find_native/', function (Request $request) {
352
352
// a simple MongoDB query that looks for a customer based on the guid
353
353
$mongodbquery = ['guid' => 'cust_2222'];
354
354
@@ -387,7 +387,7 @@ Steps
387
387
388
388
.. code-block:: php
389
389
390
- Route::get('/update_native/', function (Request $request) {
390
+ Route::get('/update_native/', function (Request $request) {
391
391
$mdb_collection = DB::connection('mongodb')->getCollection('laracoll');
392
392
$match = ['guid' => 'cust_2222'];
393
393
$update = ['$set' => ['first_name' => 'Henry', 'address.street' => '777 new street name'] ];
@@ -400,7 +400,7 @@ Steps
400
400
401
401
.. code-block:: php
402
402
403
- Route::get('/delete_native/', function (Request $request) {
403
+ Route::get('/delete_native/', function (Request $request) {
404
404
$mdb_collection = DB::connection('mongodb')->getCollection('laracoll');
405
405
$match = ['guid' => 'cust_2222'];
406
406
$result = $mdb_collection->deleteOne($match );
@@ -440,7 +440,7 @@ Steps
440
440
441
441
.. code-block:: php
442
442
443
- Route::get('/aggregate/', function (Request $request) {
443
+ Route::get('/aggregate/', function (Request $request) {
444
444
$mdb_collection = DB::connection('mongodb_mflix')->getCollection('movies');
445
445
446
446
$stage0 = ['$unwind' => ['path' => '$genres']];
0 commit comments