80 lines
2.2 KiB
PHP
80 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Student;
|
|
use App\Models\Subject;
|
|
use App\Models\Teacher;
|
|
use App\Models\User;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class TeacherService
|
|
{
|
|
public function create(array $data): Teacher
|
|
{
|
|
$user = User::create([
|
|
'email' => $data['email'],
|
|
'password' => $data['password'],
|
|
]);
|
|
$teacher = Teacher::create([
|
|
'name' => $data['name'],
|
|
'last_name' => $data['last_name'],
|
|
'middle_name' => $data['middle_name'],
|
|
'birthday' => $data['birthday'],
|
|
]);
|
|
$teacher->user()->save($user);
|
|
|
|
return $teacher;
|
|
}
|
|
|
|
public function update(Teacher $teacher, array $data): Teacher
|
|
{
|
|
$teacher->user()->update([
|
|
'email' => $data['email'],
|
|
'password' => $data['password'],
|
|
]);
|
|
|
|
$teacher->update([
|
|
'name' => $data['name'],
|
|
'last_name' => $data['last_name'],
|
|
'middle_name' => $data['middle_name'],
|
|
'birthday' => $data['birthday'],
|
|
]);
|
|
|
|
return $teacher;
|
|
}
|
|
|
|
public function delete(Teacher $teacher): void
|
|
{
|
|
$teacher->user()->delete();
|
|
$teacher->delete();
|
|
}
|
|
|
|
public function getTeachers()
|
|
{
|
|
if (Auth::user()->userable_type == Student::class) {
|
|
return Teacher::join('grade_teacher', 'teachers.id', '=', 'grade_teacher.teacher_id')
|
|
->where('grade_id', Auth::user()->userable->grade_id)
|
|
->filter()
|
|
->paginate(5)
|
|
->withQueryString();
|
|
}
|
|
|
|
return Teacher::filter()->paginate(5)->withQueryString();
|
|
}
|
|
|
|
public function getSubjects(Teacher $teacher)
|
|
{
|
|
if (Auth::user()->userable_type == Student::class) {
|
|
return Subject::join('subject_teacher', 'subject_teacher.subject_id', '=', 'subjects.id')
|
|
->join('grade_subject', 'grade_subject.subject_id', '=', 'subjects.id')
|
|
->where('grade_subject.grade_id', Auth::user()->userable->grade_id)
|
|
->where('subject_teacher.teacher_id', $teacher->id)
|
|
->get();
|
|
}
|
|
|
|
return $teacher->subjects;
|
|
}
|
|
}
|