Created
April 19, 2014 07:11
-
-
Save asaokamei/11076579 to your computer and use it in GitHub Desktop.
Eloquentのモデル生成法比較:newとcreate ref: http://qiita.com/asaokamei/items/bdad9d0f931f0840a3a9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$entity1 = new Model(); | |
$entity2 = Model::create( array() ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Blog extends Eloquent { | |
public comments() { | |
return $this->hasMany('Comment'); | |
} | |
} | |
class Comment extends Eloquent { | |
public blogs() { | |
return $this->BelongsTo('Blog'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$blog = new Blog(); | |
$comment = new Comment(); | |
$blog->comment()->save($comment); // 例外発生! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$blog = Blog::create(array()); | |
$comment = Comment::create(array()); | |
$blog->comment()->save($comment); // 問題なし! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment