40

Is there something I can put in my migrations to automatically seed the table with test data once the migration has completed?

Or do you have to seed separately?

imperium2335
  • 23,402
  • 38
  • 111
  • 190

4 Answers4

94

You can call migrate:refresh with the --seed option to automatically seed after the migrations are complete:

php artisan migrate:refresh --seed

This will rollback and re-run all your migrations and run all seeders afterwards.


Just as a little extra, you can also always use Artisan::call() to run an artisan command from inside the application:

Artisan::call('db:seed');

or

Artisan::call('db:seed', array('--class' => 'YourSeederClass'));

if you want specific seeder class.

Marty Aghajanyan
  • 12,651
  • 8
  • 35
  • 37
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • 1
    I did as you told and It was all right until it comes to production. Production freezes with this. Please see my [question](https://stackoverflow.com/questions/50458656/database-gets-stuck-in-migration-with-seeder-on-production-with-force-in-larav) for details. – Yevgeniy Afanasyev May 22 '18 at 01:49
  • 5
    Be aware, if you run this in production after you have data other than seed data in DB, you will loose that data. – Manpreet Nov 05 '18 at 05:23
60

If you don't want to delete existing data and want to seed after migrating

lukasgeiter's answer is correct for test data, but running following artisan command

php artisan migrate:refresh --seed

in production will refresh your database removing any data entered or updated from frontend.

If you want to seed your database along a migration (example rolling out an update to your application keeping existing data), like adding a new table countries along with seed data, you can do the following:

Create a database seeder example YourSeeder.php in database/seeds and your location table migration

class YourTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tablename', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',1000);
            $table->timestamps();
            $table->softDeletes();
        });

        $seeder = new YourTableSeeder();
        $seeder->run();
    }

    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
        Schema::dropIfExists('tablename');
    }
}

Run composer dump-autoload if there is a php class not found error for YourTableSeeder class.

Manpreet
  • 2,450
  • 16
  • 21
  • 2
    This should be the accepted answer imo. From within a migration this is really readable. Furthermore, if you call a command from within the migration, you would also need to add the --force flag to prevent the system for asking for confirmation in production-servers. – Rob Derks Nov 08 '18 at 12:11
  • This is what I was looking for, new table with default content without changing the hole database. – Lennon Mar 12 '19 at 13:54
6

While lukasgeiter's answer is correct, I'd like to elaborate on your second question.

Or do you have to seed separately?

Yes. Since you're talking about test data you should avoid coupling seeding with migration. Of course if this were not test data, but application data, you could always make inserting data part of the migration.

As an aside, if you want to seed your data as part of testing, you can call $this->seed() from within your Laravel test case.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
1

Tips for run migration and seeder specific file for Laravel Framework

Migrate

php artisan migrate --path=/database/migrations/fileName.php

Roolback

php artisan migrate:rollback --path=/database/migrations/fileName.php

Refresh

php artisan migrate:refresh --path=/database/migrations/fileName.php

Seeder

php artisan db:seed --class=classNameTableSeeder

Thanks