Specify dependencies in the run() method

You can specify dependencies in the run() method of your seeder.

1class DatabaseSeeder extends Seeder
2{
3 public function run()
4 {
5 $user = User::factory()->create();
6 
7 $this->callWith(EventSeeder::class, [
8 'user' => $user
9 ]);
10 }
11}
1class EventSeeder extends Seeder
2{
3 public function run(User $user)
4 {
5 Event::factory()
6 ->when($user, fn($f) => $f->for('user'))
7 ->for(Program::factory())
8 ->create();
9 }
10}

Tip given by @justsanjit

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord