Laravel Seeding Creating a Seeder


Example

To create seeders, you may use the make:seeder Artisan command. All seeders generated will be placed in the database/seeds directory.

$ php artisan make:seeder MoviesTableSeeder

Generated seeders will contain one method: run. You may insert data into your database in this method.

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class MoviesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        App\Movie::create([
            'name' => 'A New Hope',
            'year' => '1977'
        ]);

        App\Movie::create([
            'name' => 'The Empire Strikes Back',
            'year' => '1980'
        ]);
    }
}

You will generally want to call all your seeders inside the DatabaseSeeder class.

Once you're done writing the seeders, use the db:seed command. This will run DatabaseSeeder's run function.

$ php artisan db:seed

You may also specify to run a specific seeder class to run individually using the --class option.

$ php artisan db:seed --class=UserSeeder

If you want to rollback and rerun all migrations, and then reseed:

$ php artisan migrate:refresh --seed

The migrate:refresh --seed command is a shortcut to these 3 commands:

$ php artisan migrate:reset     # rollback all migrations
$ php artisan migrate           # run migrations
$ php artisan db:seed           # run seeders