From c44e69bf80dd3c6b31f2055c5d21afe017cab9d7 Mon Sep 17 00:00:00 2001 From: "H.J. van Meerveld" Date: Sat, 25 Aug 2012 22:09:20 +0200 Subject: [PATCH] improved code to insert related models --- laravel/documentation/database/eloquent.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/laravel/documentation/database/eloquent.md b/laravel/documentation/database/eloquent.md index e9eb32a1..cbcf83c9 100644 --- a/laravel/documentation/database/eloquent.md +++ b/laravel/documentation/database/eloquent.md @@ -299,7 +299,7 @@ ## Inserting Related Models $post = Post::find(1); - $post->comments()->insert($comment); + $comment = $post->comments()->insert($comment); 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. @@ -323,7 +323,7 @@ ### Inserting Related Models (Many-To-Many) $user = User::find(1); - $user->roles()->insert($role); + $role = $user->roles()->insert($role); Now, when the Role is inserted, not only is the Role inserted into the "roles" table, but a record in the intermediate table is also inserted for you. It couldn't be easier!