I know this is an old post, but since it appears in a Google search, I thought I would share some knowledge here. @ erin-geyer noted that mixing migrations and seeders can be a headache, and @justamartin contrasts with what you sometimes need / need data to be filled in as part of your deployment.
I would take one more step and say that sometimes it is desirable to be able to consistently deploy data changes so that you can, for example, deploy at the stage, see that everything is fine, and then deploy the same results with confidence (and do not need to remember to complete some manual step).
However, importance remains for seed separation and migration, as these are two related but different problems. Our team has compromised the creation of migrations called seeders. It looks like this:
public function up() { Artisan::call( 'db:seed', [ '--class' => 'SomeSeeder', '--force' => true ] ); }
This allows you to run the seed once, like a carry. You can also implement logic that prevents or enhances behavior. For example:
public function up() { if ( SomeModel::count() < 10 ) { Artisan::call( 'db:seed', [ '--class' => 'SomeSeeder', '--force' => true ] ); } }
This will obviously conditionally execute your seeder if there are less than 10 SomeModels. This is useful if you want to turn on the seeder as a standard seeder that runs when you call artisan db:seed , and also when migrating so that you don't double. You can also create a reverse seeder so that rollbacks work as expected, such as
public function down() { Artisan::call( 'db:seed', [ '--class' => 'ReverseSomeSeeder', '--force' => true ] ); }
The second parameter --force is required to start the seeder in the production environment.
darrylkuhn Sep 30 '15 at 2:43 2015-09-30 02:43
source share