Skip to content

Seed a module with factories and seeders

To populate the posts table of the Blog module with test data, define a factory, call it from a seeder, and run module:seed. You need the Post model and factory from Add Eloquent models to a module and the migrated posts table.

module:make already generated Modules/Blog/database/seeders/BlogDatabaseSeeder.php with an empty run() method.

  1. Define the factory. If you do not have one yet, generate Modules/Blog/database/factories/PostFactory.php with php artisan module:make-factory Post Blog.

    Modules/Blog/database/factories/PostFactory.php
    public function definition(): array
    {
    return [
    'title' => fake()->sentence(),
    'body' => fake()->paragraph(),
    ];
    }
  2. Generate a seeder. The command appends Seeder automatically.

    Terminal window
    php artisan module:make-seed Post Blog

    This creates Modules/Blog/database/seeders/PostSeeder.php. Add your factory call to run():

    Modules/Blog/database/seeders/PostSeeder.php
    <?php
    namespace Modules\Blog\Database\Seeders;
    use Illuminate\Database\Seeder;
    use Modules\Blog\Models\Post;
    class PostSeeder extends Seeder
    {
    public function run(): void
    {
    Post::factory()->count(3)->create();
    }
    }
  3. Call PostSeeder from the module’s main database seeder.

    Modules/Blog/database/seeders/BlogDatabaseSeeder.php
    <?php
    namespace Modules\Blog\Database\Seeders;
    use Illuminate\Database\Seeder;
    class BlogDatabaseSeeder extends Seeder
    {
    public function run(): void
    {
    $this->call([PostSeeder::class]);
    }
    }
  4. Run the module seeder.

    Terminal window
    php artisan module:seed Blog
    INFO Seeding module ...
    Seeding Blog Module ......................................
    INFO Seeding database.
    Modules\Blog\Database\Seeders\BlogDatabaseSeeder ............... RUNNING
    Modules\Blog\Database\Seeders\PostSeeder ....................... RUNNING
    Modules\Blog\Database\Seeders\PostSeeder ................... <time> DONE
    Modules\Blog\Database\Seeders\BlogDatabaseSeeder ............ <time> DONE
    INFO Module [Blog] seeded.

    If seeding fails or you decline the production confirmation, the command prints an error and exits with status 1.

  5. Check the result.

    Terminal window
    php artisan tinker --execute="echo Modules\Blog\Models\Post::count();"

    The count increases by 3 on each run (starting at 3 on an empty table).

Pass --class with a class in the module’s seeder namespace to run a single seeder:

Terminal window
php artisan module:seed Blog --class=PostSeeder

Add --seed to module:migrate to seed the module right after migrating:

Terminal window
php artisan module:migrate Blog --seed

Call the module’s main seeder from database/seeders/DatabaseSeeder.php to include it in php artisan db:seed:

database/seeders/DatabaseSeeder.php
public function run(): void
{
$this->call([
\Modules\Blog\Database\Seeders\BlogDatabaseSeeder::class,
]);
}