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.
-
Define the factory. If you do not have one yet, generate
Modules/Blog/database/factories/PostFactory.phpwithphp artisan module:make-factory Post Blog.Modules/Blog/database/factories/PostFactory.php public function definition(): array{return ['title' => fake()->sentence(),'body' => fake()->paragraph(),];} -
Generate a seeder. The command appends
Seederautomatically.Terminal window php artisan module:make-seed Post BlogThis creates
Modules/Blog/database/seeders/PostSeeder.php. Add your factory call torun():Modules/Blog/database/seeders/PostSeeder.php <?phpnamespace 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();}} -
Call
PostSeederfrom the module’s main database seeder.Modules/Blog/database/seeders/BlogDatabaseSeeder.php <?phpnamespace Modules\Blog\Database\Seeders;use Illuminate\Database\Seeder;class BlogDatabaseSeeder extends Seeder{public function run(): void{$this->call([PostSeeder::class]);}} -
Run the module seeder.
Terminal window php artisan module:seed BlogINFO Seeding module ...Seeding Blog Module ......................................INFO Seeding database.Modules\Blog\Database\Seeders\BlogDatabaseSeeder ............... RUNNINGModules\Blog\Database\Seeders\PostSeeder ....................... RUNNINGModules\Blog\Database\Seeders\PostSeeder ................... <time> DONEModules\Blog\Database\Seeders\BlogDatabaseSeeder ............ <time> DONEINFO Module [Blog] seeded.If seeding fails or you decline the production confirmation, the command prints an error and exits with status
1. -
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
3on an empty table).
Run one seeder
Section titled “Run one seeder”Pass --class with a class in the module’s seeder namespace to run a single seeder:
php artisan module:seed Blog --class=PostSeederSeed after migrating
Section titled “Seed after migrating”Add --seed to module:migrate to seed the module right after migrating:
php artisan module:migrate Blog --seedSeed modules with db:seed
Section titled “Seed modules with db:seed”Call the module’s main seeder from database/seeders/DatabaseSeeder.php to include it in php artisan db:seed:
public function run(): void{ $this->call([ \Modules\Blog\Database\Seeders\BlogDatabaseSeeder::class, ]);}Related
Section titled “Related”module:make-factoryandmodule:make-seedmodule:seed: the seeder lookup order and all options.- Add Eloquent models to a module
- Create and run module migrations