*/ class StudentFactory extends Factory { /** * Define the model's default state. * * @return array */ public function definition(): array { return [ 'school_id' => function () { return School::factory()->create()->id; }, 'first_name' => fake()->firstName(), 'last_name' => fake()->lastName(), 'grade' => rand(7, 12), ]; } public function forSchool(School $school) { return $this->state(function (array $attributes) use ($school) { return [ 'school_id' => $school->id, ]; }); } }