1
0
mirror of https://github.com/robonen/education-project.git synced 2026-03-20 02:44:31 +00:00

Получение предметов и учеников класса, классов учителя

This commit is contained in:
nikden13
2020-12-09 00:03:57 +07:00
parent 5b0ba96adf
commit 3c93886fef
10 changed files with 51 additions and 9 deletions

View File

@@ -51,7 +51,7 @@ class SchoolClassController extends Controller
}
//добавление, изменение классного руководителя
public function addClassroomTeacher(SchoolClass $class, Request $request)
public function addTeacher(SchoolClass $class, Request $request)
{
$teacher = Teacher::findOrfail((int)$request->input('teacher_id'));
$class->classroom_teacher = $teacher->id;
@@ -59,4 +59,26 @@ class SchoolClassController extends Controller
return response()->json(SchoolClass::find($class->id), 200);
}
//получение всех учеников в классе
public function getStudents(SchoolClass $class)
{
$students = $class->students;
$studentsOnlyFIO = [];
foreach ($students as $student) {
array_push($studentsOnlyFIO, $student->only('id', 'name', 'surname', 'patronymic'));
}
return response()->json($studentsOnlyFIO, 200);
}
//получение всех предметов для класса
public function getSubjects(SchoolClass $class)
{
$subjects = $class->subjects;
$subjectExceptPivot = [];
foreach ($subjects as $subject) {
array_push($subjectExceptPivot, collect($subject)->except('pivot'));
}
return response()->json($subjectExceptPivot, 200);
}
}

View File

@@ -44,4 +44,14 @@ class TeacherController extends Controller
$teacher->update($request->all());
return response()->json($teacher, 200);
}
public function getClasses(Teacher $teacher)
{
$timetables = $teacher->timetables;
$classes = [];
foreach ($timetables as $timetable) {
array_push($classes, $timetable->schoolClass->only('id','number','letter'));
}
return response()->json(collect($classes)->unique(), 200);
}
}

View File

@@ -17,12 +17,13 @@ class SchoolClass extends Model
public function students()
{
return $this->hasMany(Student::class);
return $this->hasMany(Student::class, 'class_id');
}
public function subjects()
{
return $this->belongsToMany(Subject::class, 'academic_plans')->withPivot('hours_per_week', 'hours_per_year');
return $this->belongsToMany(Subject::class, 'academic_plans', 'class_id')
->withPivot('hours_per_week', 'hours_per_year');
}
}

View File

@@ -20,7 +20,8 @@ class Subject extends Model
public function schoolClasses()
{
return $this->belongsToMany(SchoolClass::class, 'academic_plans')->withPivot('hours_per_week', 'hours_per_year');
return $this->belongsToMany(SchoolClass::class, 'academic_plans', 'class_id')
->withPivot('hours_per_week', 'hours_per_year');
}
}

View File

@@ -19,4 +19,9 @@ class Teacher extends Model
return $this->hasOne(SchoolClass::class, 'classroom_teacher');
}
public function timetables()
{
return $this->hasMany(Timetable::class);
}
}

View File

@@ -15,7 +15,7 @@ class CreateTeachersTable extends Migration
{
Schema::create('teachers', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('user_id')->unique();
$table->string('photo')->nullable();
$table->string('name')->nullable();
$table->string('surname')->nullable();

View File

@@ -15,7 +15,7 @@ class CreateParentsTable extends Migration
{
Schema::create('parents', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('user_id')->unique();
$table->string('photo')->nullable();
$table->string('name')->nullable();
$table->string('surname')->nullable();

View File

@@ -15,7 +15,7 @@ class CreateStudentsTable extends Migration
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('user_id')->unique();
$table->string('photo')->nullable();
$table->string('name')->nullable();
$table->string('surname')->nullable();

View File

@@ -15,7 +15,7 @@ class CreateHeadTeachersTable extends Migration
{
Schema::create('head_teachers', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('user_id')->unique();
$table->string('photo')->nullable();
$table->string('name')->nullable();
$table->string('surname')->nullable();

View File

@@ -12,6 +12,7 @@ Route::group(['prefix' => 'auth'], function () {
Route::apiResource('headteachers', 'Users\HeadTeacherController');
Route::apiResource('teachers', 'Users\TeacherController');
Route::get('teacher/{teacher}/classes', 'Users\TeacherController@getClasses');
Route::apiResource('students', 'Users\StudentController');
@@ -20,7 +21,9 @@ Route::apiResource('parents', 'Users\ParenttController');
Route::apiResource('subjects', 'SubjectController');
Route::apiResource('classes', 'SchoolClassController');
Route::post('classes/{class}/teacher', 'SchoolClassController@addClassroomTeacher');
Route::post('classes/{class}/teacher', 'SchoolClassController@addTeacher');
Route::get('classes/{class}/students', 'SchoolClassController@getStudents');
Route::get('classes/{class}/subjects', 'SchoolClassController@getSubjects');
Route::apiResource('themes', 'ThemeController');