create(); $grades = Grade::factory(10)->create(); $subjects = Subject::factory(10)->create(); $scores = ScoreEnum::cases(); $teachers->each(function ($teacher) { $user = User::factory()->create(); $teacher->user()->save($user); }); $grades->each(function ($grade) use ($subjects, $teachers, $scores){ $grade->subjects()->sync($subjects); $grade->teachers()->sync($teachers); $students = Student::factory(10)->create([ 'grade_id' => $grade->id, ])->each(function ($student) use ($scores) { $user = User::factory()->create(); $student->user()->save($user); }); $lessons = Lesson::factory(30)->create([ 'grade_id' => $grade->id, 'subject_id' => $grade->subjects->random()->id, 'teacher_id' => $grade->teachers->random()->id, ]); $students->each(function ($student) use ($lessons, $scores) { $lessons->each(function ($lesson) use ($student, $scores) { $student->lessons() ->syncWithoutDetaching([$lesson->id => ['score' => $scores[array_rand($scores)]]]); }); }); }); $subjects->each(function ($subject) use ($teachers) { $subject->teachers()->sync($teachers->random(2)); }); } }