Skip to content

Commit f6e43f5

Browse files
committed
Adding embedsOne readme section
1 parent a045497 commit f6e43f5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,33 @@ Again, you may override the conventional local key by passing a second argument
412412

413413
return $this->embedsMany('Book', 'local_key');
414414

415+
### EmbedsOne Relations
416+
417+
There is also an EmbedsOne relation, which works similar to the EmbedsMany relation, but only stores one embedded model.
418+
419+
use Jenssegers\Mongodb\Model as Eloquent;
420+
421+
class Book extends Eloquent {
422+
423+
public function author()
424+
{
425+
return $this->embedsOne('Author');
426+
}
427+
428+
}
429+
430+
Now we can access the book's author through the dynamic property:
431+
432+
$author = Book::first()->author;
433+
434+
Inserting and updating embedded documents works just like the `embedsMany` relation:
435+
436+
$author = new Author(array('name' => 'John Doe'));
437+
438+
$book = Books::first();
439+
440+
$author = $user->author()->save($author);
441+
415442
### MySQL Relations
416443

417444
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

Comments
 (0)