Skip to content

Instantly share code, notes, and snippets.

@aejnsn
Created August 26, 2015 06:02
Show Gist options
  • Save aejnsn/7a3e670fa94b47604c73 to your computer and use it in GitHub Desktop.
Save aejnsn/7a3e670fa94b47604c73 to your computer and use it in GitHub Desktop.
Laravel Model Factories with Collections
factory(Group::class, 10)->create()->each(function (Group $group) {
$group->users()->saveMany(factory(User::class, 10)->make());
});
@aejnsn
Copy link
Author

aejnsn commented Aug 26, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment