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
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -359,6 +359,49 @@ The belongsToMany relation will not use a pivot "table", but will push id's to a
359
359
360
360
Other relations are not yet supported, but may be added in the future. Read more about these relations on http://four.laravel.com/docs/eloquent#relationships
361
361
362
+
### EmbedsMany Relations
363
+
364
+
If you want to embed documents, rather than referencing them, you can use the `embedsMany` relation:
365
+
366
+
use Jenssegers\Mongodb\Model as Eloquent;
367
+
368
+
class User extends Eloquent {
369
+
370
+
public function books()
371
+
{
372
+
return $this->embedsMany('Book');
373
+
}
374
+
375
+
}
376
+
377
+
Now we can access the user's books through the dynamic property:
378
+
379
+
$books = User::first()->books;
380
+
381
+
When using embedded documents, there will also be an inverse relation available:
382
+
383
+
$user = $book->user;
384
+
385
+
Inserting and updating embedded documents works just like the `belongsTo` relation:
386
+
387
+
$book = new Book(array('title' => 'A Game of Thrones'));
388
+
389
+
$user = User::first();
390
+
391
+
$book = $user->books()->save($book);
392
+
393
+
You can remove an embedded document by using the `destroy()` method:
394
+
395
+
$book = $user->books()->first();
396
+
397
+
$user->books()->destroy($book->_id);
398
+
// or
399
+
$user->books()->destroy($book);
400
+
401
+
Again, you may override the conventional local key by passing a second argument to the embedsMany method:
402
+
403
+
return $this->embedsMany('Book', 'local_key');
404
+
362
405
### MySQL Relations
363
406
364
407
If you're using a hybrid MongoDB and SQL setup, you're in luck! The model will automatically return a MongoDB- or SQL-relation based on the type of the related model. Of course, if you want this functionality to work both ways, your SQL-models will need to extend `Jenssegers\Eloquent\Model`. Note that this functionality only works for hasOne, hasMany and belongsTo relations.
0 commit comments