CourseWork/database/seeders/DatabaseSeeder.php

61 lines
1.8 KiB
PHP
Raw Normal View History

2024-04-15 16:23:05 +04:00
<?php
namespace Database\Seeders;
2024-06-15 20:55:20 +04:00
use App\Enums\ScoreEnum;
use App\Models\Grade;
use App\Models\Lesson;
use App\Models\Student;
use App\Models\Subject;
use App\Models\Teacher;
2024-04-15 16:23:05 +04:00
use App\Models\User;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
2024-06-15 20:55:20 +04:00
$teachers = Teacher::factory(15)->create();
$grades = Grade::factory(10)->create();
$subjects = Subject::factory(10)->create();
$scores = ScoreEnum::cases();
2024-04-15 16:23:05 +04:00
2024-06-15 20:55:20 +04:00
$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));
});
2024-04-15 16:23:05 +04:00
}
}