task-6 (Factories and seeders) #6
23
database/factories/GradeFactory.php
Normal file
23
database/factories/GradeFactory.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Grade>
|
||||||
|
*/
|
||||||
|
class GradeFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => 'Класс' . fake()->unique()->numberBetween(1,100)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
29
database/factories/LessonFactory.php
Normal file
29
database/factories/LessonFactory.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Enums\TypeLesson;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Lesson>
|
||||||
|
*/
|
||||||
|
class LessonFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
$typeLessonCases = TypeLesson::cases();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'name' => 'Занятие' . fake()->unique()->numberBetween(),
|
||||||
|
'description' => fake()->text(200),
|
||||||
|
'type' => $typeLessonCases[array_rand($typeLessonCases)],
|
||||||
|
'lesson_date' => fake()->date(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
28
database/factories/StudentFactory.php
Normal file
28
database/factories/StudentFactory.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Student>
|
||||||
|
*/
|
||||||
|
class StudentFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
$faker = \Faker\Factory::create('ru_RU');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'name' => $faker->firstNameMale,
|
||||||
|
'last_name' => $faker->lastNameMale,
|
||||||
|
'middle_name' => $faker->middleNameMale,
|
||||||
|
'birthday' => $faker->date(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
23
database/factories/SubjectFactory.php
Normal file
23
database/factories/SubjectFactory.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Subject>
|
||||||
|
*/
|
||||||
|
class SubjectFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => 'Предмет' . fake()->unique()->numberBetween(1, 100)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
28
database/factories/TeacherFactory.php
Normal file
28
database/factories/TeacherFactory.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Teacher>
|
||||||
|
*/
|
||||||
|
class TeacherFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
$faker = \Faker\Factory::create('ru_RU');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'name' => $faker->firstNameMale,
|
||||||
|
'last_name' => $faker->lastNameMale,
|
||||||
|
'middle_name' => $faker->middleNameMale,
|
||||||
|
'birthday' => $faker->date(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -24,7 +24,6 @@ class UserFactory extends Factory
|
|||||||
public function definition(): array
|
public function definition(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name' => fake()->name(),
|
|
||||||
'email' => fake()->unique()->safeEmail(),
|
'email' => fake()->unique()->safeEmail(),
|
||||||
'email_verified_at' => now(),
|
'email_verified_at' => now(),
|
||||||
'password' => static::$password ??= Hash::make('password'),
|
'password' => static::$password ??= Hash::make('password'),
|
||||||
|
@ -2,8 +2,13 @@
|
|||||||
|
|
||||||
namespace Database\Seeders;
|
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 App\Models\User;
|
||||||
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
class DatabaseSeeder extends Seeder
|
class DatabaseSeeder extends Seeder
|
||||||
@ -13,11 +18,43 @@ class DatabaseSeeder extends Seeder
|
|||||||
*/
|
*/
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
// User::factory(10)->create();
|
$teachers = Teacher::factory(15)->create();
|
||||||
|
$grades = Grade::factory(10)->create();
|
||||||
|
$subjects = Subject::factory(10)->create();
|
||||||
|
$scores = ScoreEnum::cases();
|
||||||
|
|
||||||
User::factory()->create([
|
$teachers->each(function ($teacher) {
|
||||||
'name' => 'Test User',
|
$user = User::factory()->create();
|
||||||
'email' => 'test@example.com',
|
$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));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user