diff --git a/bundles/docs/pages/changes.md b/bundles/docs/pages/changes.md index 84e695d2..2cdaae61 100644 --- a/bundles/docs/pages/changes.md +++ b/bundles/docs/pages/changes.md @@ -21,6 +21,7 @@ - [Added `to_array` method to the base Eloquent model](/docs/database/eloquent#to-array). - [Added `$hidden` static variable to the base Eloquent model](/docs/database/eloquent#to-array). - [Added `sync` method to has\_many\_and\_belongs\_to Eloquent relationship](/docs/database/eloquent#sync-method). +- [Added `save` method to has\_many Eloquent relationship](/docs/database/eloquent#has-many-save). - Fixed the passing of strings into the Input::except method. - Fixed replacement of optional parameters in URL::transpose method. - Improved View performance by only loading contents from file once. diff --git a/bundles/docs/pages/database/eloquent.md b/bundles/docs/pages/database/eloquent.md index b0e499fb..0706b7d9 100644 --- a/bundles/docs/pages/database/eloquent.md +++ b/bundles/docs/pages/database/eloquent.md @@ -290,6 +290,20 @@ Let's assume you have a **Post** model that has many comments. Often you may wan When inserting related models through their parent model, the foreign key will automatically be set. So, in this case, the "post_id" was automatically set to "1" on the newly inserted comment. + +When working with `has_many` relationships, you may use the `save` method to insert / update related models: + + $comments = array( + array('message' => 'A new comment.'), + array('message' => 'A second comment.'), + ); + + $post = Post::find(1); + + $post->comments()->save($comments); + +### Inserting Related Models (Many-To-Many) + This is even more helpful when working with many-to-many relationships. For example, consider a **User** model that has many roles. Likewise, the **Role** model may have many users. So, the intermediate table for this relationship has "user_id" and "role_id" columns. Now, let's insert a new Role for a User: $role = new Role(array('title' => 'Admin'));