1
votes

I'm confused about using them, so want to make sure I'm using them correctly and get to know if there is any other use case". Here are my use cases:

1- In unit tests

public function test_index_returns_view()
{
    ...
    $streets = factory(Street::class, 10)->make();
    ...
}

2- when I'm developing a new app, run the seeders like this to puplate DB

php artisan migrate:fresh --seed

3- for inserting some records in production, put something like this in a migration

Artisan::call('db:seed', array('--class' => 'YourSeederClass'));
1
Seeding on production should only be done on the initial setup. In the question you're mentioning the 3 different situations. Unit Tests like that should also be on a test environment. No need to test the application on funcitonal stuff, if it doesnt function dont deploy. Point 2 is fine, using the migrate command is fine. 3. Like i said, use seeders for inital installation of the application.Collin

1 Answers

1
votes

The three scenarios you outlined are all good use cases.

Essentially, you can use factories to generate any data you want for seeding. You can also take advantage of factory states and events to generate complex relationships between multiple models.

There is technically nothing stopping you from using factories to generate production data if you really wanted to, it just isn't something you will see in any offical documention.

I have personally used it before to populate the initial values of newly migrated tables on production environments (throughly tested on my local environment beforehand of course!)