Created
August 26, 2015 06:02
-
-
Save aejnsn/7a3e670fa94b47604c73 to your computer and use it in GitHub Desktop.
Laravel Model Factories with Collections
This file contains hidden or 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
factory(Group::class, 10)->create()->each(function (Group $group) { | |
$group->users()->saveMany(factory(User::class, 10)->make()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This effectively says I want 10 groups with 10 users in each group. Eloquent's saveMany() method will save a Collection onto a relationship defined on the model. Model factories are clean.