improved code to insert related models

This commit is contained in:
H.J. van Meerveld 2012-08-25 22:09:20 +02:00
parent 833cf12ce3
commit c44e69bf80
1 changed files with 2 additions and 2 deletions

View File

@ -299,7 +299,7 @@ ## Inserting Related Models
$post = Post::find(1); $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. 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 = 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! 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!