CourseWork/database/seeders/DatabaseSeeder.php
2024-06-23 17:02:18 +04:00

103 lines
3.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Database\Seeders;
use App\Enums\ScoreEnum;
use App\Models\Grade;
use App\Models\Lesson;
use App\Models\Student;
use App\Models\Subject;
use App\Models\Teacher;
use App\Models\User;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
$namesSubjects = [
'Русский язык',
'Математика',
'Английский язык',
'Биология',
'Химия',
'Литература',
'География',
'История',
'Обществознание',
'Информатика',
];
$subjects = collect();
foreach ($namesSubjects as $name) {
$subjects->push(Subject::factory()->create([
'name' => $name,
]));
}
$letterGrades = ['A', 'Б', 'В',];
$numberGrades = 11;
$grades = collect();
while($numberGrades > 0) {
foreach ($letterGrades as $letter) {
$grades->push(Grade::factory()->create([
'name' => $numberGrades . $letter,
]));
}
$numberGrades--;
}
$teachers = Teacher::factory(15)->create();
$scores = ScoreEnum::cases();
$teachers->each(function ($teacher) {
$user = User::factory()->create();
$teacher->user()->save($user);
});
$teacher = Teacher::factory()->create();
$teacher->user()->save(User::factory()->create(['email' => 'teacher@mail']));
$teacher->grades()->attach($grades->pluck('id'));
$student = Student::factory()->create(['grade_id' => $grades->first()->id]);
$student->user()->save(User::factory()->create(['email' => 'student@mail']));
$grade = $student->grade;
$lessons = collect();
$subjects->each(function ($item) use ($lessons, $teacher, $student, $grade) {
$teacher->subjects()->attach($item);
$grade->subjects()->attach($item);
$lessons->push(Lesson::factory(5)->create([
'description' => 'Выполнение задания №3 на 87 стр. учебника',
'grade_id' => $student->grade_id,
'subject_id' => $item->id,
'teacher_id' => $teacher->id,
]));
});
$grades->each(function ($grade) use ($subjects, $teachers, $scores){
Student::factory(30)->create([
'grade_id' => $grade->id,
])->each(function ($student) use ($scores) {
$user = User::factory()->create();
$student->user()->save($user);
});
});
$grade->students->each(function ($student) use ($grade, $scores) {
$grade->lessons->each(function ($lesson) use ($student, $scores) {
$student->lessons()
->syncWithoutDetaching([
$lesson->id => ['score' => $scores[array_rand($scores)]]
]);
});
});
}
}