Skip to content

Commit

Permalink
Added seeding example
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Feb 14, 2016
1 parent 2816de7 commit 608b7da
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,18 @@ $fm->define('Bar')->addDefinition('baz', Faker::date('Y-m-d'));

### Creating And Seeding

The `create` function will create and save your model, and will also save anything you generate with the `Factory` generator too. If you want to create multiple instances, check out the seed `seed` function, which accepts an additional argument at the start which is the number of models to generate in the process. The `seed` function will effectively be calling the `create` function over and over. Also, a reminder that the `instance` function is still available if you don't want database persistence.
The `create` function will create and save your model, and will also save anything you generate with the `Factory` generator too. If you want to create multiple instances, check out the seed `seed` function, which accepts an additional argument at the start which is the number of models to generate in the process. The `seed` function will effectively be calling the `create` function over and over.

For example, let's create a user model and associate multiple location and email models to each one. Each email will also have multiple token models.
```php
$user = $fm->create('User');
$profiles = $fm->seed(5, 'Location', ['user_id' => $user->id]);
$emails = $fm->seed(100, 'Email', ['user_id' => $user->id]);

foreach ($emails as $email) {
$tokens = $fm->seed(50, 'Token', ['email_id' => $email->id]);
}
```

You may encounter the following exceptions:
* `League\FactoryMuffin\Exceptions\ModelNotFoundException` will be thrown if the model class defined is not found.
Expand All @@ -171,6 +182,8 @@ There are 5 other helper functions available:
* You may call `isSaved` with an instance of a model to check if it's saved.
* You may call `isPendingOrSaved` with an instance of a model to check if it will be saved, or is already saved.

Also, a reminder that the `instance` function is still available if you don't want database persistence.

### Deleting

You can delete all your saved models with the `deleteSaved` function. Please note that your saved models will be deleted in the reverse order they were saved to ensure relationships behave correctly.
Expand Down

1 comment on commit 608b7da

@saada
Copy link

@saada saada commented on 608b7da Feb 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! Thank you! 👍

Please sign in to comment.