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
An Eloquent model and Query builder with support for MongoDB, inspired by LMongo, but using the original Laravel methods. *This library extends the original Laravel classes, so it uses exactly the same methods.*
6
+
An Eloquent model and Query builder with support for MongoDB, using the original Laravel API. *This library extends the original Laravel classes, so it uses exactly the same methods.*
7
+
8
+
### Upgrading from v1 to v2
9
+
10
+
In this new version, embedded documents are no longer saved to the parent model using an attribute with a leading underscore. If you have a relation like `embedsMany('Book')`, these books are now stored under `$model['books']` instead of `$model['_books']`. This was changed to make embedded relations less confusing for new developers.
11
+
12
+
If you want to upgrade to this new version without having to change all your existing database objects, you can modify your embedded relations to use a non-default local key including the underscore:
13
+
14
+
$this->embedsMany('Book', '_books');
15
+
16
+
Read the full changelog at https://github.com/jenssegers/laravel-mongodb/releases/tag/v2.0.0
7
17
8
18
Installation
9
19
------------
@@ -65,7 +75,7 @@ Tell your model to use the MongoDB model and set the collection (alias for table
65
75
66
76
}
67
77
68
-
If you are using a different database driver as the default one, you will need to specify the mongodb connection within your model by changing the `connection` property:
78
+
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:
69
79
70
80
use Jenssegers\Mongodb\Model as Eloquent;
71
81
@@ -94,15 +104,15 @@ This will allow you to use your registered alias like:
94
104
Query Builder
95
105
-------------
96
106
97
-
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.
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.
@@ -258,15 +268,15 @@ You may also specify additional columns to update:
258
268
259
269
When soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record. To enable soft deletes for a model, apply the SoftDeletingTrait to the model:
260
270
261
-
use Jenssegers\Mongodb\Eloquent\SoftDeletingTrait;
271
+
use Jenssegers\Mongodb\Eloquent\SoftDeletingTrait;
262
272
263
-
class User extends Eloquent {
273
+
class User extends Eloquent {
264
274
265
-
use SoftDeletingTrait;
275
+
use SoftDeletingTrait;
266
276
267
-
protected $dates = ['deleted_at'];
277
+
protected $dates = ['deleted_at'];
268
278
269
-
}
279
+
}
270
280
271
281
For more information check http://laravel.com/docs/eloquent#soft-deleting
272
282
@@ -314,7 +324,40 @@ Matches documents that satisfy a JavaScript expression. For more information che
314
324
315
325
### Inserts, updates and deletes
316
326
317
-
All basic insert, update, delete and select methods should be implemented.
327
+
Inserting, updating and deleting records works just like the original Eloquent.
328
+
329
+
**Saving a new model**
330
+
331
+
$user = new User;
332
+
$user->name = 'John';
333
+
$user->save();
334
+
335
+
You may also use the create method to save a new model in a single line:
336
+
337
+
User::create(array('name' => 'John'));
338
+
339
+
**Updating a model**
340
+
341
+
o update a model, you may retrieve it, change an attribute, and use the save method.
*There is also support for upsert operations, check https://github.com/jenssegers/laravel-mongodb#mongodb-specific-operations*
348
+
349
+
**Deleting a model**
350
+
351
+
To delete a model, simply call the delete method on the instance:
352
+
353
+
$user = User::first();
354
+
$user->delete();
355
+
356
+
Or deleting a model by its key:
357
+
358
+
User::destroy('517c43667db388101e00000f');
359
+
360
+
For more information about model manipulation, check http://laravel.com/docs/eloquent#insert-update-delete
318
361
319
362
### Dates
320
363
@@ -386,7 +429,7 @@ Other relations are not yet supported, but may be added in the future. Read more
386
429
387
430
### EmbedsMany Relations
388
431
389
-
If you want to embed documents, rather than referencing them, you can use the `embedsMany` relation:
432
+
If you want to embed models, rather than referencing them, you can use the `embedsMany` relation. This relation is similar to the `hasMany` relation, but embeds the models inside the parent object.
390
433
391
434
use Jenssegers\Mongodb\Model as Eloquent;
392
435
@@ -399,52 +442,71 @@ If you want to embed documents, rather than referencing them, you can use the `e
399
442
400
443
}
401
444
402
-
Now we can access the user's books through the dynamic property:
445
+
You access the embedded models through the dynamic property:
403
446
404
447
$books = User::first()->books;
405
448
406
-
When using embedded documents, there will also be an inverse relation available:
449
+
The inverse relation is auto*magically* available, you don't need to define this reverse relation.
407
450
408
451
$user = $book->user;
409
452
410
-
Inserting and updating embedded documents works just like the `belongsTo` relation:
453
+
Inserting and updating embedded models works similar to the `hasMany` relation:
411
454
412
455
$book = new Book(array('title' => 'A Game of Thrones'));
413
456
414
457
$user = User::first();
415
458
416
459
$book = $user->books()->save($book);
460
+
// or
461
+
$book = $user->books()->create(array('title' => 'A Game of Thrones'))
417
462
418
-
You can remove an embedded document by using the `destroy()` method:
463
+
You can update embedded models using their `save` method (available since release 2.0.0):
419
464
420
465
$book = $user->books()->first();
421
466
422
-
$user->books()->destroy($book->_id);
467
+
$book->title = 'A Game of Thrones';
468
+
469
+
$book->save();
470
+
471
+
You can remove an embedded model by using the `destroy` method on the relation, or the `delete` method on the model (available since release 2.0.0):
472
+
473
+
$book = $user->books()->first();
474
+
475
+
$book->delete();
423
476
// or
424
477
$user->books()->destroy($book);
425
478
426
-
If you want to add or remove embedded documents, without persistence, you can use the `associate` and `dissociate` methods. To write the changes to the database, save the parent object:
479
+
If you want to add or remove an embedded model, without touching the database, you can use the `associate` and `dissociate` methods. To eventually write the changes to the database, save the parent object:
427
480
428
481
$user->books()->associate($book);
482
+
429
483
$user->save();
430
484
431
-
Again, you may override the conventional local key by passing a second argument to the embedsMany method:
485
+
Like other relations, embedsMany assumes the local key of the relationship based on the model name. You can override the default local key by passing a second argument to the embedsMany method:
432
486
433
487
return $this->embedsMany('Book', 'local_key');
434
488
435
-
When using embedded documents, they will be stored in a _relation attribute of the parent document. This attribute is hidden by default when using `toArray` or `toJson`. If you want the attribute to be exposed, add it to `$exposed` property definition to your model:
489
+
Embedded relations will return a Collection of embedded items instead of a query builder. To allow a more query-like behavior, embedded relations will return a modified version of the Collection class with support for the following **additional** operations:
436
490
437
-
use Jenssegers\Mongodb\Model as Eloquent;
491
+
- where($key, $operator, $value)
492
+
- whereIn($key, $values) and whereNotIn($key, $values)
493
+
- whereBetween($key, $values) and whereNotBetween($key, $values)
494
+
- whereNull($key) and whereNotNull($key)
495
+
- orderBy($key, $direction)
496
+
- oldest() and latest()
497
+
- limit($value)
498
+
- offset($value)
499
+
- skip($value)
438
500
439
-
class User extends Eloquent {
501
+
This allows you to execute simple queries on the collection results:
**Note:** Because embedded models are not stored in a separate collection, you can not query all of embedded models. You will always have to access them through the parent model.
444
506
445
507
### EmbedsOne Relations
446
508
447
-
There is also an EmbedsOne relation, which works similar to the EmbedsMany relation, but only stores one embedded model.
509
+
The embedsOne relation is similar to the EmbedsMany relation, but only embeds a single model.
448
510
449
511
use Jenssegers\Mongodb\Model as Eloquent;
450
512
@@ -457,17 +519,31 @@ There is also an EmbedsOne relation, which works similar to the EmbedsMany relat
457
519
458
520
}
459
521
460
-
Now we can access the book's author through the dynamic property:
522
+
You access the embedded models through the dynamic property:
461
523
462
524
$author = Book::first()->author;
463
525
464
-
Inserting and updating embedded documents works just like the `embedsMany` relation:
526
+
Inserting and updating embedded models works similar to the `hasOne` relation:
465
527
466
528
$author = new Author(array('name' => 'John Doe'));
0 commit comments